public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/106500] New: ICE on invalid argument to c_sizeof()
@ 2022-08-01 19:20 kargl at gcc dot gnu.org
  2022-08-01 19:47 ` [Bug fortran/106500] ICE on function as an " kargl at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: kargl at gcc dot gnu.org @ 2022-08-01 19:20 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106500

            Bug ID: 106500
           Summary: ICE on invalid argument to c_sizeof()
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kargl at gcc dot gnu.org
  Target Milestone: ---

This invalid program,

program foo
   use iso_c_binding
   real, allocatable :: r(:)
   allocate(r(4))
   r = [1,2,3,4]
   print '(I0)', size(r), c_sizeof(real(r, c_float))
end program foo

leads to 

troutmask:kargl[230] /home/sgk/bin/gfcx -c a.f90
f951: internal compiler error: gfc_find_array_ref(): No ref found
0x8a9dbf gfc_report_diagnostic
        ../../gccx/gcc/fortran/error.cc:883
0x8a9dbf gfc_internal_error(char const*, ...)
        ../../gccx/gcc/fortran/error.cc:1503
0x874c6f gfc_find_array_ref(gfc_expr*, bool)
        ../../gccx/gcc/fortran/array.cc:2779
0x875c12 is_c_interoperable
        ../../gccx/gcc/fortran/check.cc:5271
0x87e918 gfc_check_c_sizeof(gfc_expr*)
        ../../gccx/gcc/fortran/check.cc:5294
0x8c2c3a check_specific
        ../../gccx/gcc/fortran/intrinsic.cc:4805
0x8cb8ad gfc_intrinsic_func_interface(gfc_expr*, int)
        ../../gccx/gcc/fortran/intrinsic.cc:5042
0x92f727 resolve_unknown_f
        ../../gccx/gcc/fortran/resolve.cc:2990
0x92f727 resolve_function
        ../../gccx/gcc/fortran/resolve.cc:3347
0x92f727 gfc_resolve_expr(gfc_expr*)
        ../../gccx/gcc/fortran/resolve.cc:7187
0x9378f3 gfc_resolve_expr(gfc_expr*)
        ../../gccx/gcc/fortran/resolve.cc:7154
0x9378f3 gfc_resolve_code(gfc_code*, gfc_namespace*)
        ../../gccx/gcc/fortran/resolve.cc:11966
0x93af03 gfc_resolve_blocks(gfc_code*, gfc_namespace*)
        ../../gccx/gcc/fortran/resolve.cc:10965
0x937761 gfc_resolve_code(gfc_code*, gfc_namespace*)
        ../../gccx/gcc/fortran/resolve.cc:11956
0x93acf8 resolve_codes
        ../../gccx/gcc/fortran/resolve.cc:17609
0x93adc4 gfc_resolve(gfc_namespace*)
        ../../gccx/gcc/fortran/resolve.cc:17644
0x91a260 resolve_all_program_units
        ../../gccx/gcc/fortran/parse.cc:6604
0x91a260 gfc_parse_file()
        ../../gccx/gcc/fortran/parse.cc:6860
0x96d8c7 gfc_be_parse_file
        ../../gccx/gcc/fortran/f95-lang.cc:229

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

* [Bug fortran/106500] ICE on function as an argument to c_sizeof()
  2022-08-01 19:20 [Bug fortran/106500] New: ICE on invalid argument to c_sizeof() kargl at gcc dot gnu.org
@ 2022-08-01 19:47 ` kargl at gcc dot gnu.org
  2022-09-07 17:15 ` gscfq@t-online.de
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: kargl at gcc dot gnu.org @ 2022-08-01 19:47 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106500

--- Comment #1 from kargl at gcc dot gnu.org ---
Changed subject as it's unclear whether it is invalid or not.  In any case, 
this patch allows the code to compile and it produces the expected output.

% gfcx -o z a.f90
% ./z
4
16


diff --git a/gcc/fortran/check.cc b/gcc/fortran/check.cc
index 0c2cb50c6a7..b9d460f133e 100644
--- a/gcc/fortran/check.cc
+++ b/gcc/fortran/check.cc
@@ -5266,6 +5266,9 @@ is_c_interoperable (gfc_expr *expr, const char **msg,
bool c_loc, bool c_f_ptr)
       return false;
     }

+  if ( expr->expr_type == EXPR_FUNCTION)
+    return true;
+
   if (!c_loc && expr->rank > 0 && expr->expr_type != EXPR_ARRAY)
     {
       gfc_array_ref *ar = gfc_find_array_ref (expr);

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

* [Bug fortran/106500] ICE on function as an argument to c_sizeof()
  2022-08-01 19:20 [Bug fortran/106500] New: ICE on invalid argument to c_sizeof() kargl at gcc dot gnu.org
  2022-08-01 19:47 ` [Bug fortran/106500] ICE on function as an " kargl at gcc dot gnu.org
@ 2022-09-07 17:15 ` gscfq@t-online.de
  2022-09-07 18:13 ` kargl at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: gscfq@t-online.de @ 2022-09-07 17:15 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106500

G. Steinmetz <gscfq@t-online.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |gscfq@t-online.de

--- Comment #2 from G. Steinmetz <gscfq@t-online.de> ---
The following examples are presumably valid code,
don't use a function, nor alloc, but should be related :


$ cat z1.f90
program p
   use iso_c_binding
   integer :: a(2) = 0
   print *, c_sizeof((a))
   print *, c_sizeof(2*a)
end


$ cat z2.f90
program p
   use iso_c_binding
   character(1) :: a(2) = '0'
   print *, c_sizeof((a))
   print *, c_sizeof(a)
end


$ gfortran-13-20220904 -c z1.f90
f951: internal compiler error: gfc_find_array_ref(): No ref found
0x7f04f9 gfc_report_diagnostic
        ../../gcc/fortran/error.cc:883
0x7f2077 gfc_internal_error(char const*, ...)
        ../../gcc/fortran/error.cc:1503
0x7c029c gfc_find_array_ref(gfc_expr*, bool)
        ../../gcc/fortran/array.cc:2779
0x7c1cc2 is_c_interoperable
        ../../gcc/fortran/check.cc:5271
0x7c8e86 gfc_check_c_sizeof(gfc_expr*)
        ../../gcc/fortran/check.cc:5294
0x809459 do_check
        ../../gcc/fortran/intrinsic.cc:4792
0x809459 check_specific
        ../../gcc/fortran/intrinsic.cc:4805
0x8114e4 gfc_intrinsic_func_interface(gfc_expr*, int)
        ../../gcc/fortran/intrinsic.cc:5042
0x866aa8 resolve_unknown_f
        ../../gcc/fortran/resolve.cc:2990
0x866aa8 resolve_function
        ../../gcc/fortran/resolve.cc:3347
0x866aa8 gfc_resolve_expr(gfc_expr*)
        ../../gcc/fortran/resolve.cc:7194
0x86cdac gfc_resolve_expr(gfc_expr*)
        ../../gcc/fortran/resolve.cc:7161
0x86cdac gfc_resolve_code(gfc_code*, gfc_namespace*)
        ../../gcc/fortran/resolve.cc:11973
0x86b83f gfc_resolve_blocks(gfc_code*, gfc_namespace*)
        ../../gcc/fortran/resolve.cc:10972
0x86bb98 gfc_resolve_code(gfc_code*, gfc_namespace*)
        ../../gcc/fortran/resolve.cc:11963
0x86e927 resolve_codes
        ../../gcc/fortran/resolve.cc:17616
0x86e9ee gfc_resolve(gfc_namespace*)
        ../../gcc/fortran/resolve.cc:17651
0x8567d4 resolve_all_program_units
        ../../gcc/fortran/parse.cc:6605
0x8567d4 gfc_parse_file()
        ../../gcc/fortran/parse.cc:6861
0x8a50df gfc_be_parse_file
        ../../gcc/fortran/f95-lang.cc:229

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

* [Bug fortran/106500] ICE on function as an argument to c_sizeof()
  2022-08-01 19:20 [Bug fortran/106500] New: ICE on invalid argument to c_sizeof() kargl at gcc dot gnu.org
  2022-08-01 19:47 ` [Bug fortran/106500] ICE on function as an " kargl at gcc dot gnu.org
  2022-09-07 17:15 ` gscfq@t-online.de
@ 2022-09-07 18:13 ` kargl at gcc dot gnu.org
  2024-04-05 17:18 ` anlauf at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: kargl at gcc dot gnu.org @ 2022-09-07 18:13 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106500

kargl at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
           Priority|P3                          |P4
   Last reconfirmed|                            |2022-09-07

--- Comment #3 from kargl at gcc dot gnu.org ---
(In reply to G. Steinmetz from comment #2)
> The following examples are presumably valid code,
> don't use a function, nor alloc, but should be related :
> 
> 
> $ cat z1.f90
> program p
>    use iso_c_binding
>    integer :: a(2) = 0
>    print *, c_sizeof((a))
>    print *, c_sizeof(2*a)
> end
> 
> 
> $ cat z2.f90
> program p
>    use iso_c_binding
>    character(1) :: a(2) = '0'
>    print *, c_sizeof((a))
>    print *, c_sizeof(a)
> end
> 

The above testcases compile with this patch.

diff --git a/gcc/fortran/array.cc b/gcc/fortran/array.cc
index bbdb5b392fc..f5fed223a23 100644
--- a/gcc/fortran/array.cc
+++ b/gcc/fortran/array.cc
@@ -2766,7 +2766,24 @@ gfc_find_array_ref (gfc_expr *e, bool allow_null)
 {
   gfc_ref *ref;

-  for (ref = e->ref; ref; ref = ref->next)
+  if (e->expr_type == EXPR_OP && !e->ref)
+    {
+      gfc_expr *op1, *op2;
+      op1 = e->value.op.op1;
+      op2 = e->value.op.op2;
+      if (!op2)
+       {
+         if (op1->rank == 0)
+           return NULL;
+         ref = op1->ref;
+       }
+      else
+       ref = op2->ref;
+    }
+  else
+    ref = e->ref;
+
+  for (; ref; ref = ref->next)
     if (ref->type == REF_ARRAY
        && (ref->u.ar.type == AR_FULL || ref->u.ar.type == AR_SECTION))
       break;

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

* [Bug fortran/106500] ICE on function as an argument to c_sizeof()
  2022-08-01 19:20 [Bug fortran/106500] New: ICE on invalid argument to c_sizeof() kargl at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2022-09-07 18:13 ` kargl at gcc dot gnu.org
@ 2024-04-05 17:18 ` anlauf at gcc dot gnu.org
  2024-04-08 19:03 ` anlauf at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: anlauf at gcc dot gnu.org @ 2024-04-05 17:18 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106500

anlauf at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |ice-on-valid-code
                 CC|                            |anlauf at gcc dot gnu.org

--- Comment #4 from anlauf at gcc dot gnu.org ---
I've looked some time into this and have the impression we have the checks
wrong.

E.g. testcase gfortran.dg/c_sizeof_6.f90 expects as error:

gcc/testsuite/gfortran.dg/c_sizeof_6.f90:11:16:

   11 |    i = c_sizeof(str2(1:3)) ! { dg-error "must be an interoperable data"
}
      |                1
Error: 'x' argument of 'c_sizeof' intrinsic at (1) must be an interoperable
data entity: Only whole-arrays are interoperable


First, other compilers accept the code (Intel, NAG, Nvidia, flang), and also
the other testcases in the PR, plus they give the expected answers.

Second, the standard has:

! F2023:18.2.3.8  C_SIZEOF (X)
!
! X shall be a data entity with interoperable type and type parameters,
! and shall not be an assumed-size array, an assumed-rank array that
! is associated with an assumed-size array, an unallocated allocatable
! variable, or a pointer that is not associated.

and

! 3.41 data entity
! data object, result of the evaluation of an expression, or the
! result of the execution of a function reference

So, array sections could be interoperable, expressions, function results.

What is unclear to me: when is an expression interoperable?
Or rather when is it *not*?

(Note that the standard text has not essentially changed since F2008,
so we got this too strict from the beginning.)

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

* [Bug fortran/106500] ICE on function as an argument to c_sizeof()
  2022-08-01 19:20 [Bug fortran/106500] New: ICE on invalid argument to c_sizeof() kargl at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2024-04-05 17:18 ` anlauf at gcc dot gnu.org
@ 2024-04-08 19:03 ` anlauf at gcc dot gnu.org
  2024-04-08 19:58 ` anlauf at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: anlauf at gcc dot gnu.org @ 2024-04-08 19:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106500

--- Comment #5 from anlauf at gcc dot gnu.org ---
(In reply to anlauf from comment #4)
> What is unclear to me: when is an expression interoperable?
> Or rather when is it *not*?
> 
> (Note that the standard text has not essentially changed since F2008,
> so we got this too strict from the beginning.)

Actually the following interp is very helpful:

https://j3-fortran.org/doc/year/22/22-101r1.txt

And while working on a fix, I found the following invalid code ICEing:

subroutine s ()
  use, intrinsic :: iso_c_binding
  implicit none
  type(c_ptr) :: cPtr
  call c_f_pointer (cPtr, p0)            ! { dg-error "function returning a
pointer" }
  call c_f_pointer (cPtr, p1, shape=[2]) ! { dg-error "function returning a
pointer" }
contains
  function p0 ()
    integer, pointer :: p0
    nullify (p0)
  end
  function p1 ()
    integer, pointer :: p1(:)
    nullify (p1)
  end
end

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

* [Bug fortran/106500] ICE on function as an argument to c_sizeof()
  2022-08-01 19:20 [Bug fortran/106500] New: ICE on invalid argument to c_sizeof() kargl at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2024-04-08 19:03 ` anlauf at gcc dot gnu.org
@ 2024-04-08 19:58 ` anlauf at gcc dot gnu.org
  2024-04-10 17:04 ` cvs-commit at gcc dot gnu.org
  2024-04-10 19:49 ` anlauf at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: anlauf at gcc dot gnu.org @ 2024-04-08 19:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106500

anlauf at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |anlauf at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

--- Comment #6 from anlauf at gcc dot gnu.org ---
Submitted: https://gcc.gnu.org/pipermail/fortran/2024-April/060419.html

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

* [Bug fortran/106500] ICE on function as an argument to c_sizeof()
  2022-08-01 19:20 [Bug fortran/106500] New: ICE on invalid argument to c_sizeof() kargl at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2024-04-08 19:58 ` anlauf at gcc dot gnu.org
@ 2024-04-10 17:04 ` cvs-commit at gcc dot gnu.org
  2024-04-10 19:49 ` anlauf at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-04-10 17:04 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106500

--- Comment #7 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:ded646c91d2c0fb908faf6fa8fe1df0d7df49d16

commit r14-9893-gded646c91d2c0fb908faf6fa8fe1df0d7df49d16
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Tue Apr 9 23:07:59 2024 +0200

    Fortran: fix argument checking of intrinsics C_SIZEOF, C_F_POINTER
[PR106500]

    The interpretation of the F2018 standard regarding valid arguments to the
    intrinsic C_SIZEOF(X) was clarified in an edit to 18-007r1:

      https://j3-fortran.org/doc/year/22/22-101r1.txt

    loosening restrictions and giving examples.  The F2023 text has:

    ! F2023:18.2.3.8  C_SIZEOF (X)
    !
    !   X shall be a data entity with interoperable type and type parameters,
    !   and shall not be an assumed-size array, an assumed-rank array that
    !   is associated with an assumed-size array, an unallocated allocatable
    !   variable, or a pointer that is not associated.

    where

    ! 3.41 data entity
    !   data object, result of the evaluation of an expression, or the
    !   result of the execution of a function reference

    Update the checking code for interoperable arguments accordingly, and
extend
    to reject functions returning pointer as FPTR argument to C_F_POINTER.

    gcc/fortran/ChangeLog:

            PR fortran/106500
            * check.cc (is_c_interoperable): Fix checks for C_SIZEOF.
            (gfc_check_c_f_pointer): Reject function returning a pointer as
FPTR,
            and improve an error message.

    gcc/testsuite/ChangeLog:

            PR fortran/106500
            * gfortran.dg/c_sizeof_6.f90: Remove wrong dg-error.
            * gfortran.dg/sizeof_2.f90: Adjust pattern.
            * gfortran.dg/c_f_pointer_tests_9.f90: New test.
            * gfortran.dg/c_sizeof_7.f90: New test.

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

* [Bug fortran/106500] ICE on function as an argument to c_sizeof()
  2022-08-01 19:20 [Bug fortran/106500] New: ICE on invalid argument to c_sizeof() kargl at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2024-04-10 17:04 ` cvs-commit at gcc dot gnu.org
@ 2024-04-10 19:49 ` anlauf at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: anlauf at gcc dot gnu.org @ 2024-04-10 19:49 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106500

anlauf at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
   Target Milestone|---                         |14.0
             Status|ASSIGNED                    |RESOLVED

--- Comment #8 from anlauf at gcc dot gnu.org ---
Fixed on mainline for gcc-14.

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

end of thread, other threads:[~2024-04-10 19:49 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-01 19:20 [Bug fortran/106500] New: ICE on invalid argument to c_sizeof() kargl at gcc dot gnu.org
2022-08-01 19:47 ` [Bug fortran/106500] ICE on function as an " kargl at gcc dot gnu.org
2022-09-07 17:15 ` gscfq@t-online.de
2022-09-07 18:13 ` kargl at gcc dot gnu.org
2024-04-05 17:18 ` anlauf at gcc dot gnu.org
2024-04-08 19:03 ` anlauf at gcc dot gnu.org
2024-04-08 19:58 ` anlauf at gcc dot gnu.org
2024-04-10 17:04 ` cvs-commit at gcc dot gnu.org
2024-04-10 19:49 ` anlauf at gcc dot gnu.org

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).