public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* avoid null ptr deref in cselib_record_sets
@ 2018-12-05  6:50 Alexandre Oliva
  2018-12-05  8:32 ` Jakub Jelinek
  2018-12-14 23:20 ` [committed] Fix valgrind error in cselib_record_sets (PR rtl-optimization/88478) Jakub Jelinek
  0 siblings, 2 replies; 3+ messages in thread
From: Alexandre Oliva @ 2018-12-05  6:50 UTC (permalink / raw)
  To: gcc-patches

Jeff Law tells me h8300-elf fails gcc.c-torture/compile/pr49029.c
with -O2 -g -mint32 -mh.  This patch fixes it.

The problem is that strict low part handling in cselib_record_sets
assumes src_elt is not NULL.  That src_elt is taken from a strict low
part set, but it won't always have a src_elt to begin with.  In this
case, it's because src is a volatile MEM; we don't record values for
those.

Although we could fix the problem by testing for a NULL src_elt before
creating the zero extends corresponding to strict low part sets of
formerly const0_rtx REGs, there's no point in recording the additional
set that we won't be able to use anyway.

We could still record that the whole register has a zero-extend of
the value stored in the narrower-mode strict low part of the register,
but is that of any use?  I guess not, but if we find otherwise, we can
change that later.

Jeff tested this with a cross compiler to h8300-elf, and several other
native and cross toolchains IIUC.  I'm regstrapping it myself on i686-
and x86_64-linux-gnu.  Ok to install?


for  gcc/ChangeLog

	* cselib.c (cselib_record_sets): Skip strict low part sets
	with NULL src_elt.
---
 gcc/cselib.c |    1 +
 1 file changed, 1 insertion(+)

diff --git a/gcc/cselib.c b/gcc/cselib.c
index 6d3a4078c689..4a68439455fd 100644
--- a/gcc/cselib.c
+++ b/gcc/cselib.c
@@ -2616,6 +2616,7 @@ cselib_record_sets (rtx_insn *insn)
 	 preserves the upper bits that di:SI=zero_extend(flags:CCNO<=0).  */
       scalar_int_mode mode;
       if (dest != orig
+	  && sets[i].src_elt
 	  && cselib_record_sets_hook
 	  && REG_P (dest)
 	  && HARD_REGISTER_P (dest)

-- 
Alexandre Oliva, freedom fighter   https://FSFLA.org/blogs/lxo
Be the change, be Free!         FSF Latin America board member
GNU Toolchain Engineer                Free Software Evangelist
Hay que enGNUrecerse, pero sin perder la terGNUra jamás-GNUChe

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: avoid null ptr deref in cselib_record_sets
  2018-12-05  6:50 avoid null ptr deref in cselib_record_sets Alexandre Oliva
@ 2018-12-05  8:32 ` Jakub Jelinek
  2018-12-14 23:20 ` [committed] Fix valgrind error in cselib_record_sets (PR rtl-optimization/88478) Jakub Jelinek
  1 sibling, 0 replies; 3+ messages in thread
From: Jakub Jelinek @ 2018-12-05  8:32 UTC (permalink / raw)
  To: Alexandre Oliva; +Cc: gcc-patches

On Wed, Dec 05, 2018 at 04:50:19AM -0200, Alexandre Oliva wrote:
> Jeff tested this with a cross compiler to h8300-elf, and several other
> native and cross toolchains IIUC.  I'm regstrapping it myself on i686-
> and x86_64-linux-gnu.  Ok to install?
> 
> 
> for  gcc/ChangeLog
> 
> 	* cselib.c (cselib_record_sets): Skip strict low part sets
> 	with NULL src_elt.

Ok, thanks.

> --- a/gcc/cselib.c
> +++ b/gcc/cselib.c
> @@ -2616,6 +2616,7 @@ cselib_record_sets (rtx_insn *insn)
>  	 preserves the upper bits that di:SI=zero_extend(flags:CCNO<=0).  */
>        scalar_int_mode mode;
>        if (dest != orig
> +	  && sets[i].src_elt
>  	  && cselib_record_sets_hook
>  	  && REG_P (dest)
>  	  && HARD_REGISTER_P (dest)

	Jakub

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [committed] Fix valgrind error in cselib_record_sets (PR rtl-optimization/88478)
  2018-12-05  6:50 avoid null ptr deref in cselib_record_sets Alexandre Oliva
  2018-12-05  8:32 ` Jakub Jelinek
@ 2018-12-14 23:20 ` Jakub Jelinek
  1 sibling, 0 replies; 3+ messages in thread
From: Jakub Jelinek @ 2018-12-14 23:20 UTC (permalink / raw)
  To: Alexandre Oliva; +Cc: gcc-patches

Hi!

On Wed, Dec 05, 2018 at 04:50:19AM -0200, Alexandre Oliva wrote:
> 	* cselib.c (cselib_record_sets): Skip strict low part sets
> 	with NULL src_elt.
> ---
>  gcc/cselib.c |    1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/gcc/cselib.c b/gcc/cselib.c
> index 6d3a4078c689..4a68439455fd 100644
> --- a/gcc/cselib.c
> +++ b/gcc/cselib.c
> @@ -2616,6 +2616,7 @@ cselib_record_sets (rtx_insn *insn)
>  	 preserves the upper bits that di:SI=zero_extend(flags:CCNO<=0).  */
>        scalar_int_mode mode;
>        if (dest != orig
> +	  && sets[i].src_elt
>  	  && cselib_record_sets_hook
>  	  && REG_P (dest)
>  	  && HARD_REGISTER_P (dest)

This regresses following testcase under valgrind on x86_64-linux.

The problem is that sets[i].src_elt is only conditionally initialized before
this:

      /* We don't know how to record anything but REG or MEM.  */
      if (REG_P (dest)
          || (MEM_P (dest) && cselib_record_memory))
        {
          rtx src = sets[i].src;
          if (cond)
            src = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond, src, dest);
          sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1, VOIDmode);
...
        }

otherwise it is uninitialized.  So, we need to test it after REG_P (dest)
two lines after it.

Tested on x86_64-linux, committed to trunk as obvious.

2018-12-15  Jakub Jelinek  <jakub@redhat.com>

	PR rtl-optimization/88478
	* cselib.c (cselib_record_sets): Move sets[i].src_elt tests
	after REG_P (dest) test.

	* g++.dg/opt/pr88478.C: New test.

--- gcc/cselib.c.jj	2018-12-07 00:23:15.722987285 +0100
+++ gcc/cselib.c	2018-12-15 00:10:16.779762222 +0100
@@ -2616,10 +2616,10 @@ cselib_record_sets (rtx_insn *insn)
 	 preserves the upper bits that di:SI=zero_extend(flags:CCNO<=0).  */
       scalar_int_mode mode;
       if (dest != orig
-	  && sets[i].src_elt
 	  && cselib_record_sets_hook
 	  && REG_P (dest)
 	  && HARD_REGISTER_P (dest)
+	  && sets[i].src_elt
 	  && is_a <scalar_int_mode> (GET_MODE (dest), &mode)
 	  && n_sets + n_strict_low_parts < MAX_SETS)
 	{
--- gcc/testsuite/g++.dg/opt/pr88478.C.jj	2018-12-15 00:14:14.427927166 +0100
+++ gcc/testsuite/g++.dg/opt/pr88478.C	2018-12-15 00:12:20.762761443 +0100
@@ -0,0 +1,17 @@
+// PR rtl-optimization/88478
+// { dg-do compile }
+// { dg-options "-O2" }
+
+struct A {
+  bool b;
+  int s;
+  template <typename T, typename U>
+  A (T, U) {}
+};
+enum F {} f;
+
+A
+foo ()
+{
+  return A (false, f);
+}


	Jakub

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-12-14 23:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-05  6:50 avoid null ptr deref in cselib_record_sets Alexandre Oliva
2018-12-05  8:32 ` Jakub Jelinek
2018-12-14 23:20 ` [committed] Fix valgrind error in cselib_record_sets (PR rtl-optimization/88478) 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).