Blame SOURCES/gcc8-rh2137448.patch

45c72a
commit 8b89515caca5149329c0cd20485e69e2d0f879d4
45c72a
Author: Marek Polacek <polacek@redhat.com>
45c72a
Date:   Wed Dec 7 13:44:38 2022 -0500
45c72a
45c72a
    strlen: Use D_S_U in maybe_set_strlen_range
45c72a
    
45c72a
    This patch fixes #2137448 where the customer uses strlen on a buffer
45c72a
    that was filled by converting the buffer to a struct and copying a string
45c72a
    into a flexible array member of the struct.
45c72a
    
45c72a
    This regressed with r262438 in the sense that the strlen was folded to 0.
45c72a
    The strlen=0 result started with
45c72a
    https://gcc.gnu.org/pipermail/gcc-patches/2018-July/501912.html
45c72a
    https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;h=715fcd73b66c639d9e0e3f3ef9c6ff9d621d7131
45c72a
    which seems like an undesirable change.  It was fixed (back to strlen=3) by
45c72a
    https://gcc.gnu.org/legacy-ml/gcc-patches/2019-01/msg00069.html
45c72a
    https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;h=d4bf69750d31d08068f8242225b8fa06cdf11411
45c72a
    but the changes are not backportable.
45c72a
    
45c72a
    Instead, this patch makes maybe_set_strlen_range use DECL_SIZE_UNIT
45c72a
    rather than TYPE_SIZE_UNIT, fixing the regression.
45c72a
    
45c72a
    I could never reproduce the problem in C, only C++.  C/C++ represent array
45c72a
    type domains differently: C has
45c72a
    
45c72a
      char[0:]
45c72a
    
45c72a
    but C++
45c72a
    
45c72a
      char[0:18446744073709551615]
45c72a
    
45c72a
    I'm not sure if that explains it.  In any case, I put the new test into
45c72a
    c-c++-common/.
45c72a
    
45c72a
    Also, the original test had
45c72a
    
45c72a
      printf("strlen = %zu\n", strlen(q->name));
45c72a
    
45c72a
    so naturally, for the testsuite, I wanted to convert that into
45c72a
    
45c72a
      if (strlen(q->name) != ...)
45c72a
         __builtin_abort ();
45c72a
    
45c72a
    but then I could no longer reproduce the problem.  After some poking
45c72a
    I realized I want -fno-early-inlining.
45c72a
    
45c72a
    Co-authored-by: Jakub Jelinek <jakub@redhat.com>
45c72a
45c72a
diff --git a/gcc/testsuite/c-c++-common/torture/strlenopt-1.c b/gcc/testsuite/c-c++-common/torture/strlenopt-1.c
45c72a
new file mode 100644
45c72a
index 00000000000..e8c11044119
45c72a
--- /dev/null
45c72a
+++ b/gcc/testsuite/c-c++-common/torture/strlenopt-1.c
45c72a
@@ -0,0 +1,38 @@
45c72a
+/* { dg-do run } */
45c72a
+/* { dg-options "-fno-early-inlining" } */
45c72a
+
45c72a
+#define FORTIFY_SOURCE 2
45c72a
+
45c72a
+struct S {
45c72a
+ char skip;
45c72a
+ char name[0];
45c72a
+};
45c72a
+
45c72a
+static char static_buf[4];
45c72a
+
45c72a
+static void
45c72a
+print_name_len(void *p)
45c72a
+{
45c72a
+  struct S *q = (struct S *) p;
45c72a
+  if (__builtin_strlen(q->name) != 2)
45c72a
+    __builtin_abort ();
45c72a
+}
45c72a
+
45c72a
+int
45c72a
+main(void)
45c72a
+{
45c72a
+  // treat static storage as struct
45c72a
+  struct S *c = (struct S *)static_buf;
45c72a
+  __builtin_strcpy(c->name, "aa");
45c72a
+
45c72a
+  // copy static storage to stack storage
45c72a
+  char stack_buf[4] = { 0 };
45c72a
+  __builtin_memcpy(stack_buf, static_buf, 4);
45c72a
+
45c72a
+  // static and stack both now contain ( 0, 'a', 'a', 0 }
45c72a
+
45c72a
+  // indirectly pass the stack storage to the length function
45c72a
+  char *s = (char *)stack_buf;
45c72a
+  print_name_len(s);
45c72a
+  return 0;
45c72a
+}
45c72a
diff --git a/gcc/tree-ssa-strlen.c b/gcc/tree-ssa-strlen.c
45c72a
index 55e82e7b638..da47046cc2a 100644
45c72a
--- a/gcc/tree-ssa-strlen.c
45c72a
+++ b/gcc/tree-ssa-strlen.c
45c72a
@@ -1200,8 +1200,11 @@ maybe_set_strlen_range (tree lhs, tree src)
45c72a
       || array_at_struct_end_p (src))
45c72a
     return;
45c72a
 
45c72a
-  tree type = TREE_TYPE (src);
45c72a
-  if (tree size = TYPE_SIZE_UNIT (type))
45c72a
+  src = get_base_address (src);
45c72a
+  if (!DECL_P (src))
45c72a
+    return;
45c72a
+
45c72a
+  if (tree size = DECL_SIZE_UNIT (src))
45c72a
     if (size && TREE_CODE (size) == INTEGER_CST)
45c72a
       {
45c72a
 	wide_int max = wi::to_wide (size);