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