Blame SOURCES/gcc8-rh2137448.patch

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