public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] tree-optimization/109170 - bogus use-after-free with __builtin_expect
@ 2023-03-17 12:18 Richard Biener
  2023-03-17 12:43 ` Jakub Jelinek
  0 siblings, 1 reply; 16+ messages in thread
From: Richard Biener @ 2023-03-17 12:18 UTC (permalink / raw)
  To: gcc-patches; +Cc: Jakub Jelinek, aldyh, amacleod

The following adds a missing range-op for __builtin_expect which
helps -Wuse-after-free to detect the case a realloc original
pointer is used when the result was NULL.

Bootstrap and regtest running on x86_64-unknown-linux-gnu, OK?

	PR tree-optimization/109170
	* gimple-range-op.cc (cfn_expect): New.
	(gimple_range_op_handler::maybe_builtin_call): Handle
	__builtin_expect.

	* gcc.dg/Wuse-after-free-pr109170.c: New testcase.
---
 gcc/gimple-range-op.cc                        | 25 +++++++++++++++++++
 .../gcc.dg/Wuse-after-free-pr109170.c         | 15 +++++++++++
 2 files changed, 40 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/Wuse-after-free-pr109170.c

diff --git a/gcc/gimple-range-op.cc b/gcc/gimple-range-op.cc
index a5d625387e7..f3d6e29e58d 100644
--- a/gcc/gimple-range-op.cc
+++ b/gcc/gimple-range-op.cc
@@ -309,6 +309,25 @@ public:
   }
 } op_cfn_constant_p;
 
+// Implement range operator for integral CFN_BUILT_IN_EXPECT.
+class cfn_expect : public range_operator
+{
+public:
+  using range_operator::fold_range;
+  virtual bool fold_range (irange &r, tree, const irange &lh,
+			   const irange &, relation_trio) const
+  {
+    r = lh;
+    return true;
+  }
+  virtual bool op1_range (irange &r, tree, const irange &lhs,
+			  const irange &, relation_trio) const
+  {
+    r = lhs;
+    return true;
+  }
+} op_cfn_expect;
+
 // Implement range operator for CFN_BUILT_IN_SIGNBIT.
 class cfn_signbit : public range_operator_float
 {
@@ -966,6 +985,12 @@ gimple_range_op_handler::maybe_builtin_call ()
       m_int = &op_cfn_parity;
       break;
 
+    case CFN_BUILT_IN_EXPECT:
+      m_valid = true;
+      m_op1 = gimple_call_arg (call, 0);
+      m_int = &op_cfn_expect;
+      break;
+
     default:
       break;
     }
diff --git a/gcc/testsuite/gcc.dg/Wuse-after-free-pr109170.c b/gcc/testsuite/gcc.dg/Wuse-after-free-pr109170.c
new file mode 100644
index 00000000000..fa7dc66d66c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wuse-after-free-pr109170.c
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -Wuse-after-free" } */
+
+unsigned long bufmax = 0;
+unsigned long __open_catalog_bufmax;
+void *realloc(void *, __SIZE_TYPE__);
+void free(void *);
+
+void __open_catalog(char *buf)
+{
+  char *old_buf = buf;
+  buf = realloc (buf, bufmax);
+  if (__builtin_expect ((buf == ((void *)0)), 0))
+    free (old_buf); /* { dg-bogus "used after" } */
+}
-- 
2.35.3

^ permalink raw reply	[flat|nested] 16+ messages in thread
* [PATCH] tree-optimization/109170 - bogus use-after-free with __builtin_expect
@ 2023-04-27 12:10 Richard Biener
  0 siblings, 0 replies; 16+ messages in thread
From: Richard Biener @ 2023-04-27 12:10 UTC (permalink / raw)
  To: gcc-patches; +Cc: Jakub Jelinek

The following generalizes the range-op for __builtin_expect
by using the fnspec machinery.

We've defered this to stage1 - bootstrapped and tested on 
x86_64-unknown-linux-gnu.

OK?

Thanks,
Richard.

	PR tree-optimization/109170
	* gimple-range-op.cc (gimple_range_op_handler::maybe_builtin_call):
	Handle __builtin_expect and similar via cfn_pass_through_arg1
	and inspecting the calls fnspec.
	* builtins.cc (builtin_fnspec): Handle BUILT_IN_EXPECT
	and BUILT_IN_EXPECT_WITH_PROBABILITY.
---
 gcc/builtins.cc        |  2 ++
 gcc/gimple-range-op.cc | 19 +++++++++++--------
 2 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/gcc/builtins.cc b/gcc/builtins.cc
index 878596c240a..bd07873a80e 100644
--- a/gcc/builtins.cc
+++ b/gcc/builtins.cc
@@ -11718,6 +11718,8 @@ builtin_fnspec (tree callee)
       case BUILT_IN_RETURN_ADDRESS:
 	return ".c";
       case BUILT_IN_ASSUME_ALIGNED:
+      case BUILT_IN_EXPECT:
+      case BUILT_IN_EXPECT_WITH_PROBABILITY:
 	return "1cX ";
       /* But posix_memalign stores a pointer into the memory pointed to
 	 by its first argument.  */
diff --git a/gcc/gimple-range-op.cc b/gcc/gimple-range-op.cc
index f7409e35a99..04e27d6aa05 100644
--- a/gcc/gimple-range-op.cc
+++ b/gcc/gimple-range-op.cc
@@ -43,6 +43,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "range.h"
 #include "value-query.h"
 #include "gimple-range.h"
+#include "attr-fnspec.h"
 
 // Given stmt S, fill VEC, up to VEC_SIZE elements, with relevant ssa-names
 // on the statement.  For efficiency, it is an error to not pass in enough
@@ -984,14 +985,16 @@ gimple_range_op_handler::maybe_builtin_call ()
       m_int = &op_cfn_parity;
       break;
 
-    case CFN_BUILT_IN_EXPECT:
-    case CFN_BUILT_IN_EXPECT_WITH_PROBABILITY:
-      m_valid = true;
-      m_op1 = gimple_call_arg (call, 0);
-      m_int = &op_cfn_pass_through_arg1;
-      break;
-
     default:
-      break;
+      {
+	unsigned arg;
+	if (gimple_call_fnspec (call).returns_arg (&arg) && arg == 0)
+	  {
+	    m_valid = true;
+	    m_op1 = gimple_call_arg (call, 0);
+	    m_int = &op_cfn_pass_through_arg1;
+	  }
+	break;
+      }
     }
 }
-- 
2.35.3

^ permalink raw reply	[flat|nested] 16+ messages in thread
[parent not found: <34641.123042708104200740@us-mta-611.us.mimecast.lan>]

end of thread, other threads:[~2023-04-27 12:12 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-17 12:18 [PATCH] tree-optimization/109170 - bogus use-after-free with __builtin_expect Richard Biener
2023-03-17 12:43 ` Jakub Jelinek
2023-03-17 12:53   ` Richard Biener
2023-03-17 12:59     ` Jakub Jelinek
2023-03-17 13:55       ` Richard Biener
2023-03-17 14:03         ` Jakub Jelinek
2023-03-17 14:18           ` Richard Biener
2023-03-17 14:52             ` Jakub Jelinek
2023-03-20  8:21               ` Richard Biener
2023-03-20 12:12                 ` Richard Biener
2023-03-20 13:22                   ` Jakub Jelinek
2023-03-21  8:21                     ` Richard Biener
2023-03-21  8:23                       ` Jakub Jelinek
2023-03-17 13:59       ` Andrew MacLeod
2023-04-27 12:10 Richard Biener
     [not found] <34641.123042708104200740@us-mta-611.us.mimecast.lan>
2023-04-27 12:11 ` Jakub Jelinek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).