public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
From: Tobias Burnus <tobias@codesourcery.com>
To: Andrew Jenner <andrew@codesourcery.com>, <fortran@gcc.gnu.org>,
	gcc-patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] Fortran: fix reallocation on assignment of polymorphic variables [PR110415]
Date: Mon, 20 Nov 2023 15:48:52 +0100	[thread overview]
Message-ID: <c1088c68-2bd9-46ca-bd20-ea397ce07fdf@codesourcery.com> (raw)
In-Reply-To: <8365320d-ce39-4182-9c58-411436318437@codesourcery.com>

[-- Attachment #1: Type: text/plain, Size: 2655 bytes --]

Hi Andrew,

On 20.11.23 14:56, Andrew Jenner wrote:
> This patch adds the testcase from PR110415 and fixes the bug.

Thanks. I can confirm experimentally that it fixes the original PR.  However, if I extend
the original testcase (-2), it fails with run with valgrind or when run with -fsanitize=address.

The array testcase (-3) seems to be fine with and without your patch.

I think it makes sense to include both extended testcases and put them under
gcc/testsuite/gfortran.dg/asan/ to ensure ASAN checks for leaks and some invalid memory access.

I still should have a closer look at the -ftree-dump-original and at the patch itself.

* * *

For the '-2' testcase, ASAN (-fsanitize=undefined) shows:

==11823==ERROR: AddressSanitizer: heap-use-after-free on address 0x608000000220 at pc 0x000000405d39 bp 0x7ffcfa6eef50 sp 0x7ffcfa6eef48
WRITE of size 96 at 0x608000000220 thread T0
     #0 0x405d38 in __copy_MAIN___D gcc/testsuite/gfortran.dg/pr110415.f90:5
     #1 0x404dc0 in MAIN__ gcc/testsuite/gfortran.dg/pr110415.f90:24
     #2 0x40a82b in main gcc/testsuite/gfortran.dg/pr110415.f90:28
     #3 0x7f1e2a4281af in __libc_start_call_main (/lib64/libc.so.6+0x281af) (BuildId: bbeee08e5f56966e641c4f3ba4ea1da9d730d0ab)
     #4 0x7f1e2a428278 in __libc_start_main@@GLIBC_2.34 (/lib64/libc.so.6+0x28278) (BuildId: bbeee08e5f56966e641c4f3ba4ea1da9d730d0ab)
     #5 0x402274 in _start ../sysdeps/x86_64/start.S:115

0x608000000220 is located 0 bytes inside of 96-byte region [0x608000000220,0x608000000280)
freed by thread T0 here:
     #0 0x7f1e2b4daf35  (/lib64/libasan.so.8+0xdaf35) (BuildId: 3e1694ad218c99a8b1b69231666a27df63cf19d0)
     #1 0x404d1c in MAIN__ gcc/testsuite/gfortran.dg/pr110415.f90:24
     #2 0x40a82b in main gcc/testsuite/gfortran.dg/pr110415.f90:28
     #3 0x7f1e2a4281af in __libc_start_call_main (/lib64/libc.so.6+0x281af) (BuildId: bbeee08e5f56966e641c4f3ba4ea1da9d730d0ab)

previously allocated by thread T0 here:
     #0 0x7f1e2b4dc03f in malloc (/lib64/libasan.so.8+0xdc03f) (BuildId: 3e1694ad218c99a8b1b69231666a27df63cf19d0)
     #1 0x4042d2 in MAIN__ gcc/testsuite/gfortran.dg/pr110415.f90:22
     #2 0x40a82b in main gcc/testsuite/gfortran.dg/pr110415.f90:28
     #3 0x7f1e2a4281af in __libc_start_call_main (/lib64/libc.so.6+0x281af) (BuildId: bbeee08e5f56966e641c4f3ba4ea1da9d730d0ab)

* * *

Tobias
-----------------
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

[-- Attachment #2: pr110415-2.f90 --]
[-- Type: text/x-fortran, Size: 780 bytes --]

! { dg-do run }
!
! Contributed by Brad Richardson <everythingfunctional@protonmail.com>
!
implicit none
  type, abstract :: p
    integer :: a = 4
  end type p

  type, extends(p) :: c
    integer :: b = 7
    character(len=:), allocatable :: str, str2(:)
  end type c

  type, extends(p) :: d
    integer :: ef = 7
    character(len=:), allocatable :: str, str2(:)
  end type d

  class(p), allocatable :: a

  a = func()

  a = func2()

  a = func()

  deallocate(a)

contains
  function func2() result(a)
    class(p), allocatable :: a
    a = d()
  end function func2

  function func() result(a)
    class(p), allocatable :: a

    a = c()
    select type(a)
    type is (c)
      a%str = 'abcd'
      a%str2 = ['abcd','efgh']
    end select
  end function func
end program

[-- Attachment #3: pr110415-3.f90 --]
[-- Type: text/x-fortran, Size: 927 bytes --]

! { dg-do run }
!
! Contributed by Brad Richardson <everythingfunctional@protonmail.com>
!
implicit none
  type, abstract :: p
    integer :: a = 4
  end type p

  type, extends(p) :: c
    integer :: b = 7
    character(len=:), allocatable :: str, str2(:)
  end type c

  type, extends(p) :: d
    integer :: ef = 7
    character(len=:), allocatable :: str, str2(:)
  end type d

  class(p), allocatable :: a(:)

  a = func()

  a = func2()

  a = func()

  deallocate(a)

contains
  function func2() result(a)
    class(p), allocatable :: a(:)
    a = [d(),d()]
  end function func2

  function func() result(a)
    class(p), allocatable :: a(:)

    a = [c(),c(),c()]
    select type(a)
    type is (c)
      a(1)%str = 'abcd'
      a(2)%str = 'abc'
      a(3)%str = 'abcd4'
      a(1)%str2 = ['abcd','efgh']
      a(2)%str2 = ['bcd','fgh']
      a(3)%str2 = ['abcd6','efgh7']
    end select
  end function func
end program

      reply	other threads:[~2023-11-20 14:49 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-20 13:56 Andrew Jenner
2023-11-20 14:48 ` Tobias Burnus [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c1088c68-2bd9-46ca-bd20-ea397ce07fdf@codesourcery.com \
    --to=tobias@codesourcery.com \
    --cc=andrew@codesourcery.com \
    --cc=fortran@gcc.gnu.org \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).