Bug 92470 - CFI_address wrongly assumes that lower bounds are at zero – invalid for pointers + allocatables
Summary: CFI_address wrongly assumes that lower bounds are at zero – invalid for point...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 10.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2019-11-12 08:44 UTC by Tobias Burnus
Modified: 2019-11-14 08:28 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2019-11-12 00:00:00


Attachments
Lightly tested patch (552 bytes, patch)
2019-11-12 08:45 UTC, Tobias Burnus
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2019-11-12 08:44:32 UTC
Mentioned at https://gcc.gnu.org/ml/fortran/2019-11/msg00060.html
refers to https://github.com/j3-fortran/fortran_proposals/issues/57#issuecomment-552680503 (the two Fortran codes in this comment are identical).

The problem is the handling of the lower bound and CFI_address.

Passing a Fortran array to bind(C), one has for the bounds (F2018, 18.5.3, para 3):

"For a C descriptor of an array pointer or allocatable array, the value of the lower_bound member of each element of the dim member of the descriptor is determined by argument association, allocation, or pointer association.
For a C descriptor of a nonallocatable nonpointer object, the value of the lower_bound member of each element of the dim member of the descriptor is zero."

Hence, for allocate(A(-2:5)) - A's lower bound has also in C the value -2.
Thus, when calling
   CFI_address(dv, lb);
with lb = -2, the result should be the original unmodified "data" pointer.

The problem is that CFI_address assumes that the lower bound is 0; libgfortran/runtime/ISO_Fortran_binding.c has:
   base_addr = base_addr + (CFI_index_t)(subscripts[i] * dv->dim[i].sm);

Expected: Either add a case separation (e.g. based on the type) or unconditionally honour the lower bound value.
Comment 1 Tobias Burnus 2019-11-12 08:45:39 UTC
Created attachment 47215 [details]
Lightly tested patch
Comment 2 Tobias Burnus 2019-11-12 14:46:25 UTC
Patch: https://gcc.gnu.org/ml/gcc-patches/2019-11/msg00928.html

 * * *

While fixing it, I run into the question which lower_bound is to be expected for CFI_section and for CFI_establish, cf. https://mailman.j3-fortran.org/pipermail/j3/2019-November/thread.html#11740 (PR 89843 also talks about CFI_section.)

The current implementation of CFI_section seems to be fine (according to Bob and Bill), however, I still think it is not well specified.
Regarding CFI_establish, there is still the question what should be the value of lower_bound for CFI_attribute_other. (gfortran uses '0' for pointer as demanded by the standard and '1' otherwise (unspecified).)
Comment 3 Tobias Burnus 2019-11-12 19:33:46 UTC
Author: burnus
Date: Tue Nov 12 19:33:10 2019
New Revision: 278101

URL: https://gcc.gnu.org/viewcvs?rev=278101&root=gcc&view=rev
Log:
PR fortran/92470 Fixes for CFI_address

        libgfortran/
        PR fortran/92470
        * runtime/ISO_Fortran_binding.c (CFI_address): Handle non-zero
        lower_bound; update error message.
        (CFI_allocate): Fix comment typo.
        (CFI_establish): Fix identation, fix typos, don't check values of 'dv'
        argument.

        gcc/testsuite/
        PR fortran/92470
        * gfortran.dg/ISO_Fortran_binding_17.c: New.
        * gfortran.dg/ISO_Fortran_binding_17.f90: New.
        * gfortran.dg/ISO_Fortran_binding_1.c (elemental_mult_c, allocate_c,
        section_c, select_part_c): Update for CFI_{address} changes;
        add asserts.


Added:
    trunk/gcc/testsuite/gfortran.dg/ISO_Fortran_binding_17.c
    trunk/gcc/testsuite/gfortran.dg/ISO_Fortran_binding_17.f90
Modified:
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/ISO_Fortran_binding_1.c
    trunk/libgfortran/ChangeLog
    trunk/libgfortran/runtime/ISO_Fortran_binding.c
Comment 4 Tobias Burnus 2019-11-13 11:14:28 UTC
Author: burnus
Date: Wed Nov 13 11:13:57 2019
New Revision: 278128

URL: https://gcc.gnu.org/viewcvs?rev=278128&root=gcc&view=rev
Log:
PR fortran/92470 Fixes for CFI_address

        libgfortran/
        PR fortran/92470
        * runtime/ISO_Fortran_binding.c (CFI_establish): Set lower_bound to 0
        also for CFI_attribute_other.

        gcc/testsuite/
        PR fortran/92470
        * gfortran.dg/ISO_Fortran_binding_1.c (establish_c): Add assert for
        lower_bound == 0.


Modified:
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/ISO_Fortran_binding_1.c
    trunk/libgfortran/ChangeLog
    trunk/libgfortran/runtime/ISO_Fortran_binding.c
Comment 5 Tobias Burnus 2019-11-13 15:07:18 UTC
Author: burnus
Date: Wed Nov 13 15:06:47 2019
New Revision: 278143

URL: https://gcc.gnu.org/viewcvs?rev=278143&root=gcc&view=rev
Log:
PR fortran/92470 Fixes for CFI_address

        Backport from mainline

        libgfortran/
        2019-11-13  Tobias Burnus  <tobias@codesourcery.com>

        PR fortran/92470
        * runtime/ISO_Fortran_binding.c (CFI_establish): Set lower_bound to 0
        also for CFI_attribute_other.

        2019-11-12  Tobias Burnus  <tobias@codesourcery.com>

        PR fortran/92470
        * runtime/ISO_Fortran_binding.c (CFI_address): Handle non-zero
        lower_bound; update error message.
        (CFI_allocate): Fix comment typo.
        (CFI_establish): Fix identation, fix typos, don't check values of 'dv'
        argument.

        gcc/testsuite/
        2019-11-13  Tobias Burnus  <tobias@codesourcery.com>

        PR fortran/92470
        * gfortran.dg/ISO_Fortran_binding_1.c (establish_c): Add assert for
        lower_bound == 0.

        2019-11-12  Tobias Burnus  <tobias@codesourcery.com>

        PR fortran/92470
        * gfortran.dg/ISO_Fortran_binding_17.c: New.
        * gfortran.dg/ISO_Fortran_binding_17.f90: New.
        * gfortran.dg/ISO_Fortran_binding_1.c (elemental_mult_c, allocate_c,
        section_c, select_part_c): Update for CFI_{address} changes;
        add asserts.


Added:
    branches/gcc-9-branch/gcc/testsuite/gfortran.dg/ISO_Fortran_binding_17.c
    branches/gcc-9-branch/gcc/testsuite/gfortran.dg/ISO_Fortran_binding_17.f90
Modified:
    branches/gcc-9-branch/gcc/testsuite/ChangeLog
    branches/gcc-9-branch/gcc/testsuite/gfortran.dg/ISO_Fortran_binding_1.c
    branches/gcc-9-branch/libgfortran/ChangeLog
    branches/gcc-9-branch/libgfortran/runtime/ISO_Fortran_binding.c
Comment 6 Tobias Burnus 2019-11-13 15:07:57 UTC
FIXED for trunk/GCC 10 and GCC 9.
Comment 7 Tobias Burnus 2019-11-14 08:28:42 UTC
Author: burnus
Date: Thu Nov 14 08:02:42 2019
New Revision: 278201

URL: https://gcc.gnu.org/viewcvs?rev=278201&root=gcc&view=rev
Log:
Fix gfortran.dg/ISO_Fortran_binding_17.c using rel. #include

        PR fortran/92470
        PR fortran/92500
        * gfortran.dg/ISO_Fortran_binding_17.c: Include
        ISO_Fortran_binding.h with relative path.


Modified:
    branches/gcc-9-branch/gcc/testsuite/ChangeLog
    branches/gcc-9-branch/gcc/testsuite/gfortran.dg/ISO_Fortran_binding_17.c