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

            Bug ID: 128677
           Summary: [15/16/17 Regression] [coarray] valgrind error in
                    gfc_conv_loop_setup for coindexed section with scalar
                    subscript
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zbeekman at gmail dot com
  Target Milestone: ---

Created attachment 66141
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66141&action=edit
F90 test case

With -fcoarray=lib, gfortran reads past the end of an allocated block
when it compiles this testcase:

program p
  integer :: a(2,2)[*]
  a(1,:)[1] = 0
end program

Valgrind shows two invalid reads. Only the top frames of each error are
shown:

$ gfortran -fcoarray=lib -c pr.f90 -wrapper valgrind,-q
==203== Invalid read of size 4
==203==    at 0x2409544: __gmpz_set (in
/usr/local/libexec/gcc/x86_64-linux-gnu/15.3.0/f951)
==203==    by 0x8F9770: ??? (in
/usr/local/libexec/gcc/x86_64-linux-gnu/15.3.0/f951)
==203==    by 0x90C270: gfc_conv_loop_setup(gfc_loopinfo*, locus*) (in
/usr/local/libexec/gcc/x86_64-linux-gnu/15.3.0/f951)
==203==    by 0x90C6A7: gfc_conv_expr_descriptor(gfc_se*, gfc_expr*) (in
/usr/local/libexec/gcc/x86_64-linux-gnu/15.3.0/f951)
==203==  Address 0x4e0c6f4 is 4 bytes after a block of size 16 alloc'd
==203==    at 0x484BBA3: calloc (vg_replace_malloc.c:1675)
==203==    by 0x2286DF4: xcalloc (in
/usr/local/libexec/gcc/x86_64-linux-gnu/15.3.0/f951)
==203==    by 0x89B649: gfc_expression_rank(gfc_expr*) (in
/usr/local/libexec/gcc/x86_64-linux-gnu/15.3.0/f951)
==203==    by 0x8A4120: ??? (in
/usr/local/libexec/gcc/x86_64-linux-gnu/15.3.0/f951)
==203==    by 0x89D640: gfc_resolve_code(gfc_code*, gfc_namespace*) (in
/usr/local/libexec/gcc/x86_64-linux-gnu/15.3.0/f951)
==203==
==203== Invalid read of size 4
==203==    at 0x8103B7: ??? (in
/usr/local/libexec/gcc/x86_64-linux-gnu/15.3.0/f951)
==203==    by 0x4DFB7FF: ???
==203==    by 0x810607: gfc_free_actual_arglist(gfc_actual_arglist*) (in
/usr/local/libexec/gcc/x86_64-linux-gnu/15.3.0/f951)
==203==    by 0x8CD739: gfc_free_statements(gfc_code*) (in
/usr/local/libexec/gcc/x86_64-linux-gnu/15.3.0/f951)
==203==  Address 0x4e0c6f0 is 0 bytes after a block of size 16 alloc'd
==203==    at 0x484BBA3: calloc (vg_replace_malloc.c:1675)
==203==    by 0x2286DF4: xcalloc (in
/usr/local/libexec/gcc/x86_64-linux-gnu/15.3.0/f951)
==203==    by 0x89B649: gfc_expression_rank(gfc_expr*) (in
/usr/local/libexec/gcc/x86_64-linux-gnu/15.3.0/f951)

Compiler (official gcc:15 Docker image):

Target: x86_64-linux-gnu
Configured with: /usr/src/gcc/configure --build=x86_64-linux-gnu
--disable-multilib --enable-languages=c,c++,fortran,go
gcc version 15.3.0 (GCC)

Results with other versions and options:
- GCC 16.2.0 (gcc:16 image): the same two errors.
- GCC 17.0.0 20261108 (third-party snapshot image): the same two errors,
  with -fcoarray=lib and with -fcoarray=shared.
- GCC 14.4.0 (gcc:14 image): no errors.
- GCC 15.3.0 with -fcoarray=single: no errors.

On x86_64-apple-darwin, Homebrew GCC 15.3.0 and 16.2.0 stop with this
error:

internal compiler error: Abort trap: 6

I tested other statements with GCC 15.3.0 and valgrind. Each statement
is the only statement in program p.

Statements that cause the errors:

  integer :: a(2,2)[*]
  a(:,1)[1] = 0

  integer :: a(2,2)[*], b(2)[*]
  b(:)[1] = a(1,:)[1]

Statements that do not cause errors:

  integer :: a(2,2)[*], b(2)[*]
  b(:) = a(1,:)[1]

  integer :: a(2,2)[*], b(2)[*]
  b(:)[1] = a(2,:)

  integer :: a(2,2)[*]
  a(1:1,:)[1] = 0

I found this error with the OpenCoarrays testsuite:
https://github.com/sourceryinstitute/OpenCoarrays/issues/809

Two other users reported failures in the same test:
- MacPorts gcc15 15.2.0, arm64 macOS: "pointer being freed was not
  allocated"
- GCC 15.2, OpenBSD amd64: "GNU MP: Cannot reallocate memory"
https://github.com/sourceryinstitute/OpenCoarrays/issues/805

Reply via email to