[committed] Fortran: Avoid running into assert with -fcheck= + UBSAN [PR92621]

Message ID 8a62c1ac-8170-b1f0-2b60-ecb81004a76f@codesourcery.com
State Committed
Headers
Series [committed] Fortran: Avoid running into assert with -fcheck= + UBSAN [PR92621] |

Commit Message

Tobias Burnus Oct. 22, 2021, 9:50 p.m. UTC
  The testcase of the PR or as attached gave an ICE, but only when
compiled with -fcheck=all -fsanitize=undefined. Solution: Strip
the nop to avoid the assert failure.

Committed as r12-4632-g24e99e6ec1cc57f3660c00ff677c7feb16aa94d2

Tobias

  * * *

PS: Similar issues when using additional flags:

ICE also with -fcheck=all -fsanitize=undefined:
https://gcc.gnu.org/PR102901 ICE (segfault) when compiling pdt_13.f03 with -fcheck=all in gfc_check_pdt_dummy -> structure_alloc_comps
https://gcc.gnu.org/PR102900 ICE via gfc_class_data_get with alloc_comp_class_4.f03 or proc_ptr_52.f90 using -fcheck=all

+ runtime same flags but running the code:
https://gcc.gnu.org/PR102903 New: Invalid gfortran.dg testcases or wrong-code with -fcheck=all -fsanitize=undefined
Here, false positives might/do surely exist as do testcase bugs. (And the list is incomplete.)

+ -flto fail (not really fitting into this series):
https://gcc.gnu.org/PR102885 - [12 Regression] ICE when compiling gfortran.dg/bind_c_char_10.f90 with -flto since r12-4467-g64f9623765da3306
-----------------
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955
  

Patch

commit 24e99e6ec1cc57f3660c00ff677c7feb16aa94d2
Author: Tobias Burnus <tobias@codesourcery.com>
Date:   Fri Oct 22 23:23:06 2021 +0200

    Fortran: Avoid running into assert with -fcheck= + UBSAN
    
            PR fortran/92621
    gcc/fortran/
            * trans-expr.c (gfc_trans_assignment_1): Add STRIP_NOPS.
    
    gcc/testsuite/
            * gfortran.dg/bind-c-intent-out-2.f90: New test.

diff --git a/gcc/fortran/trans-expr.c b/gcc/fortran/trans-expr.c
index 29697e69e75..2d7f9e0fb91 100644
--- a/gcc/fortran/trans-expr.c
+++ b/gcc/fortran/trans-expr.c
@@ -11727,6 +11727,7 @@  gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
 
 	  tmp = INDIRECT_REF_P (lse.expr)
 	      ? gfc_build_addr_expr (NULL_TREE, lse.expr) : lse.expr;
+	  STRIP_NOPS (tmp);
 
 	  /* We should only get array references here.  */
 	  gcc_assert (TREE_CODE (tmp) == POINTER_PLUS_EXPR
diff --git a/gcc/testsuite/gfortran.dg/bind-c-intent-out-2.f90 b/gcc/testsuite/gfortran.dg/bind-c-intent-out-2.f90
new file mode 100644
index 00000000000..fe8f6060f1f
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/bind-c-intent-out-2.f90
@@ -0,0 +1,39 @@ 
+! { dg-do run }
+! { dg-additional-options "-fsanitize=undefined -fcheck=all" }
+
+! PR fortran/92621
+
+subroutine hello(val) bind(c)
+  use, intrinsic :: iso_c_binding, only: c_int
+
+  implicit none
+  
+  integer(kind=c_int), allocatable, intent(out) :: val(:)
+
+  allocate(val(1))
+  val = 2
+  return
+end subroutine hello
+
+program alloc_p
+
+  use, intrinsic :: iso_c_binding, only: c_int
+
+  implicit none
+
+  interface
+    subroutine hello(val) bind(c)
+      import :: c_int
+      implicit none
+      integer(kind=c_int), allocatable, intent(out) :: val(:)
+    end subroutine hello
+  end interface
+
+  integer(kind=c_int), allocatable :: a(:)
+
+  allocate(a(1))
+  a = 1
+  call hello(a)
+  stop
+
+end program alloc_p