PR fortran/103777 - ICE in gfc_simplify_maskl, at fortran/simplify.c:4918

Message ID trinity-80276ba8-ee41-4c63-86d6-f18ffc657ae8-1640037913553@3c-app-gmx-bap28
State New
Headers
Series PR fortran/103777 - ICE in gfc_simplify_maskl, at fortran/simplify.c:4918 |

Commit Message

Harald Anlauf Dec. 20, 2021, 10:05 p.m. UTC
  Dear all,

we need to check the arguments of the elemental MASKL and MASKR
intrinsics also before simplifying.

Testcase by Gerhard.  The fix is almost obvious, but I'm happy to
get feedback in case there is something I overlooked.  (There is
already a check on scalar arguments to MASKL/MASKR, which however
misses the case of array arguments.)

Regtested on x86_64-pc-linux-gnu.  OK for mainline?

Thanks,
Harald
  

Comments

Mikael Morin Dec. 21, 2021, 12:38 p.m. UTC | #1
Le 20/12/2021 à 23:05, Harald Anlauf via Fortran a écrit :
> Dear all,
> 
> we need to check the arguments of the elemental MASKL and MASKR
> intrinsics also before simplifying.
> 
> Testcase by Gerhard.  The fix is almost obvious, but I'm happy to
> get feedback in case there is something I overlooked.  (There is
> already a check on scalar arguments to MASKL/MASKR, which however
> misses the case of array arguments.)
> 
> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
> 
Your patch looks reasonable and safe.
However, I find it surprising that it’s actually needed, as gfc_check 
mask is already the check function associated to maskl and maskr in the 
definition of the symbols.  The simplification function should be called 
only when the associated check function has returned successfully, so it 
shouldn’t be necessary to call it again at simplification time.
Looking at the backtrace, it is the do_simplify call at the beginning of 
  gfc_intrinsic_func_interface that seems dubious to me, as it comes 
before all the check further down in the function and it looks redundant 
with the other simplification code after the checks.

So I’m inclined to test whether by any chance removing that call works, 
and if it doesn’t, let’s go with this patch.

Mikael
  
Harald Anlauf Jan. 6, 2022, 7:50 p.m. UTC | #2
Hi Mikael,

Am 21.12.21 um 13:38 schrieb Mikael Morin:
> Le 20/12/2021 à 23:05, Harald Anlauf via Fortran a écrit :
>> Dear all,
>>
>> we need to check the arguments of the elemental MASKL and MASKR
>> intrinsics also before simplifying.
>>
>> Testcase by Gerhard.  The fix is almost obvious, but I'm happy to
>> get feedback in case there is something I overlooked.  (There is
>> already a check on scalar arguments to MASKL/MASKR, which however
>> misses the case of array arguments.)
>>
>> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
>>
> Your patch looks reasonable and safe.
> However, I find it surprising that it’s actually needed, as gfc_check
> mask is already the check function associated to maskl and maskr in the
> definition of the symbols.  The simplification function should be called
> only when the associated check function has returned successfully, so it
> shouldn’t be necessary to call it again at simplification time.
> Looking at the backtrace, it is the do_simplify call at the beginning of
>   gfc_intrinsic_func_interface that seems dubious to me, as it comes
> before all the check further down in the function and it looks redundant
> with the other simplification code after the checks.
>
> So I’m inclined to test whether by any chance removing that call works,
> and if it doesn’t, let’s go with this patch.

Did you find the time to try your version?

> Mikael
>

Thanks,
Harald
  
Mikael Morin Jan. 6, 2022, 9:44 p.m. UTC | #3
Le 06/01/2022 à 20:50, Harald Anlauf a écrit :
> 
> Did you find the time to try your version?
> 
Not yet. But I have not (yet) forgotten about this.
  
Mikael Morin Jan. 9, 2022, 8:12 p.m. UTC | #4
Le 06/01/2022 à 22:44, Mikael Morin a écrit :
> Le 06/01/2022 à 20:50, Harald Anlauf a écrit :
>>
>> Did you find the time to try your version?
>>
> Not yet. But I have not (yet) forgotten about this.
> I have looked at it, and it enables infinite mutual recursion between 
gfc_intrinsic_func_interface and gfc_simplify_expr, so it breaks heavily.
I am still looking at it, but let’s proceed with your original patch for 
now.

Thanks.
  
Harald Anlauf Jan. 9, 2022, 9:25 p.m. UTC | #5
Am 09.01.22 um 21:12 schrieb Mikael Morin:
> Le 06/01/2022 à 22:44, Mikael Morin a écrit :
>> Le 06/01/2022 à 20:50, Harald Anlauf a écrit :
>>>
>>> Did you find the time to try your version?
>>>
>> Not yet. But I have not (yet) forgotten about this.
>> I have looked at it, and it enables infinite mutual recursion between
> gfc_intrinsic_func_interface and gfc_simplify_expr, so it breaks heavily.
> I am still looking at it, but let’s proceed with your original patch for
> now.

OK, done so.  It should not prevent a better solution later...

> Thanks.
>

Thanks,
Harald
  

Patch

From b58a44bc861ee3d1e67e3b7c949a301b6290c05c Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Mon, 20 Dec 2021 22:59:53 +0100
Subject: [PATCH] Fortran: check arguments of MASKL/MASKR intrinsics before
 simplification

gcc/fortran/ChangeLog:

	PR fortran/103777
	* simplify.c (gfc_simplify_maskr): Check validity of argument 'I'
	before simplifying.
	(gfc_simplify_maskl): Likewise.

gcc/testsuite/ChangeLog:

	PR fortran/103777
	* gfortran.dg/masklr_3.f90: New test.
---
 gcc/fortran/simplify.c                 |  6 ++++++
 gcc/testsuite/gfortran.dg/masklr_3.f90 | 14 ++++++++++++++
 2 files changed, 20 insertions(+)
 create mode 100644 gcc/testsuite/gfortran.dg/masklr_3.f90

diff --git a/gcc/fortran/simplify.c b/gcc/fortran/simplify.c
index 90067b6bbe6..b6f636d4ff1 100644
--- a/gcc/fortran/simplify.c
+++ b/gcc/fortran/simplify.c
@@ -4878,6 +4878,9 @@  gfc_simplify_maskr (gfc_expr *i, gfc_expr *kind_arg)
   bool fail = gfc_extract_int (i, &arg);
   gcc_assert (!fail);

+  if (!gfc_check_mask (i, kind_arg))
+    return &gfc_bad_expr;
+
   result = gfc_get_constant_expr (BT_INTEGER, kind, &i->where);

   /* MASKR(n) = 2^n - 1 */
@@ -4909,6 +4912,9 @@  gfc_simplify_maskl (gfc_expr *i, gfc_expr *kind_arg)
   bool fail = gfc_extract_int (i, &arg);
   gcc_assert (!fail);

+  if (!gfc_check_mask (i, kind_arg))
+    return &gfc_bad_expr;
+
   result = gfc_get_constant_expr (BT_INTEGER, kind, &i->where);

   /* MASKL(n) = 2^bit_size - 2^(bit_size - n) */
diff --git a/gcc/testsuite/gfortran.dg/masklr_3.f90 b/gcc/testsuite/gfortran.dg/masklr_3.f90
new file mode 100644
index 00000000000..eb689f0f408
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/masklr_3.f90
@@ -0,0 +1,14 @@ 
+! { dg-do compile }
+! PR fortran/103777 - ICE in gfc_simplify_maskl
+! Contributed by G.Steinmetz
+
+program p
+  print *, maskl([999])       ! { dg-error "must be less than or equal" }
+  print *, maskr([999])       ! { dg-error "must be less than or equal" }
+  print *, maskl([-999])      ! { dg-error "must be nonnegative" }
+  print *, maskr([-999])      ! { dg-error "must be nonnegative" }
+  print *, maskl([32],kind=4)
+  print *, maskl([33],kind=4) ! { dg-error "must be less than or equal" }
+  print *, maskl([64],kind=8)
+  print *, maskl([65],kind=8) ! { dg-error "must be less than or equal" }
+end
--
2.26.2