Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flang runtime uses uninitialized value, causing memory leak and segfault #880

Open
difference-scheme opened this issue Mar 8, 2020 · 2 comments
Labels

Comments

@difference-scheme
Copy link

difference-scheme commented Mar 8, 2020

The following sample code exposes the problem (this might actually have the same cause as the segfault of Issue #851. However, even if the polymorphic assignment below is replaced by sourced allocation, to eliminate the segfault, a memory leak remains present):

subroutine calc()

   implicit none
   
   type, abstract :: Base
   end type Base
   
   type, extends(Base) :: Derived
   end type Derived
   
   class(Base),    allocatable :: b
   class(Derived), allocatable :: d
   
   allocate(Derived :: d)
   
   ! polymorphic assignment
   b = d
   
end subroutine calc

program testf
   implicit none   
   call calc()
end program testf

When compiled with gfortran 9.0.1 and run using valgrind --track-origins=yes --leak-check=full ./a.out valgrind's output is:

==7303== Memcheck, a memory error detector
==7303== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==7303== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==7303== Command: ./a.out
==7303== 
==7303== 
==7303== HEAP SUMMARY:
==7303==     in use at exit: 0 bytes in 0 blocks
==7303==   total heap usage: 23 allocs, 23 frees, 13,562 bytes allocated
==7303== 
==7303== All heap blocks were freed -- no leaks are possible
==7303== 
==7303== For counts of detected and suppressed errors, rerun with: -v
==7303== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

When compiled with flang, valgrind --track-origins=yes --leak-check=full ./a.out gives:

==7309== Memcheck, a memory error detector
==7309== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==7309== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==7309== Command: ./a.out
==7309== 
==7309== Conditional jump or move depends on uninitialised value(s)
==7309==    at 0x50E73A1: get_source_and_dest_sizes (in /home/Compilers/Flang/install/lib/libflang.so)
==7309==    by 0x50E8265: f90_poly_asn_i8 (in /home/Compilers/Flang/install/lib/libflang.so)
==7309==    by 0x400CFF: calc_ (memtest.f90:17)
==7309==    by 0x400E39: MAIN_ (memtest.f90:23)
==7309==    by 0x400E75: main (in /home/Compiler_bugs/Flang_issues/Issue_8xx/a.out)
==7309==  Uninitialised value was created by a stack allocation
==7309==    at 0x4009C0: calc_ (memtest.f90:1)
==7309== 
==7309== Use of uninitialised value of size 8
==7309==    at 0x50E745E: get_source_and_dest_sizes (in /home/Compilers/Flang/install/lib/libflang.so)
==7309==    by 0x50E8265: f90_poly_asn_i8 (in /home/Compilers/Flang/install/lib/libflang.so)
==7309==    by 0x400CFF: calc_ (memtest.f90:17)
==7309==    by 0x400E39: MAIN_ (memtest.f90:23)
==7309==    by 0x400E75: main (in /home/Compiler_bugs/Flang_issues/Issue_8xx/a.out)
==7309==  Uninitialised value was created by a stack allocation
==7309==    at 0x4009C0: calc_ (memtest.f90:1)
==7309== 
==7309== Invalid read of size 1
==7309==    at 0x4C341ED: memcpy@GLIBC_2.2.5 (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7309==    by 0x50E8498: f90_poly_asn_i8 (in /home/Compilers/Flang/install/lib/libflang.so)
==7309==    by 0x400CFF: calc_ (memtest.f90:17)
==7309==    by 0x400E39: MAIN_ (memtest.f90:23)
==7309==    by 0x400E75: main (in /home/Compiler_bugs/Flang_issues/Issue_8xx/a.out)
==7309==  Address 0x47175d90 is not stack'd, malloc'd or (recently) free'd
==7309== 
==7309== 
==7309== Process terminating with default action of signal 11 (SIGSEGV)
==7309==  Access not within mapped region at address 0x47175D90
==7309==    at 0x4C341ED: memcpy@GLIBC_2.2.5 (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7309==    by 0x50E8498: f90_poly_asn_i8 (in /home/Compilers/Flang/install/lib/libflang.so)
==7309==    by 0x400CFF: calc_ (memtest.f90:17)
==7309==    by 0x400E39: MAIN_ (memtest.f90:23)
==7309==    by 0x400E75: main (in /home/Compiler_bugs/Flang_issues/Issue_8xx/a.out)
==7309==  If you believe this happened as a result of a stack
==7309==  overflow in your program's main thread (unlikely but
==7309==  possible), you can try to increase the size of the
==7309==  main thread stack using the --main-stacksize= flag.
==7309==  The main thread stack size used in this run was 16777216.
==7309== 
==7309== HEAP SUMMARY:
==7309==     in use at exit: 12,352 bytes in 3 blocks
==7309==   total heap usage: 4 allocs, 1 frees, 85,056 bytes allocated
==7309== 
==7309== 32 bytes in 1 blocks are possibly lost in loss record 1 of 3
==7309==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7309==    by 0x4F400A0: __fort_gcalloc_without_abort (in /home/Compilers/Flang/install/lib/libflang.so)
==7309==    by 0x5041ED3: __alloc04_i8 (in /home/Compilers/Flang/install/lib/libflang.so)
==7309==    by 0x5043209: f90_ptr_src_calloc04a_i8 (in /home/Compilers/Flang/install/lib/libflang.so)
==7309==    by 0x400A8F: calc_ (memtest.f90:14)
==7309==    by 0x400E39: MAIN_ (memtest.f90:23)
==7309==    by 0x400E75: main (in /home/Compiler_bugs/Flang_issues/Issue_8xx/a.out)
==7309== 
==7309== 32 bytes in 1 blocks are possibly lost in loss record 2 of 3
==7309==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7309==    by 0x4F400A0: __fort_gcalloc_without_abort (in /home/Compilers/Flang/install/lib/libflang.so)
==7309==    by 0x5041ED3: __alloc04_i8 (in /home/Compilers/Flang/install/lib/libflang.so)
==7309==    by 0x5043209: f90_ptr_src_calloc04a_i8 (in /home/Compilers/Flang/install/lib/libflang.so)
==7309==    by 0x400CC2: calc_ (memtest.f90:17)
==7309==    by 0x400E39: MAIN_ (memtest.f90:23)
==7309==    by 0x400E75: main (in /home/Compiler_bugs/Flang_issues/Issue_8xx/a.out)
==7309== 
==7309== LEAK SUMMARY:
==7309==    definitely lost: 0 bytes in 0 blocks
==7309==    indirectly lost: 0 bytes in 0 blocks
==7309==      possibly lost: 64 bytes in 2 blocks
==7309==    still reachable: 12,288 bytes in 1 blocks
==7309==         suppressed: 0 bytes in 0 blocks
==7309== Reachable blocks (those to which a pointer was found) are not shown.
==7309== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==7309== 
==7309== For counts of detected and suppressed errors, rerun with: -v
==7309== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)

Notice that the number of allocs/frees is asymmetric (4 allocs were performed, but only 1 free).

@mleair mleair added the bug label Mar 8, 2020
@mleair
Copy link
Contributor

mleair commented Mar 8, 2020

This is related to issue #851. If Base is non-empty the segfault goes away. You could also use sourced allocation as a work around.

@difference-scheme
Copy link
Author

Similar to Issue #851, the segfault doesn't go away if Base contains only some deferred procedure(s), e.g. as follows

   type, abstract :: Base
   contains
      procedure(pbase), deferred, nopass :: some_method
   end type Base
   
   abstract interface
      subroutine pbase()
      end subroutine pbase
   end interface

This is an extremely important use case: the use of such constructs is necessary for emulating subtyping polymorphism in Fortran (i.e. for emulating Java-like interfaces), which in turn is presently the only way to enable a sound OO programming style in Fortran (one that separates the interface of an OO algorithm from its implementation, and thus avoids a plethora of select type statements, see also https://github.com/j3-fortran/fortran_proposals/tree/master/proposals/run-time_polymorphism).

As long as this bug isn't fixed, flang won't allow this programming style, making Fortran OO programming unnecessarily dreadful. The same holds for the replacement of polymorphic assignment by sourced allocation. It is simply not feasible for large OO applications with large numbers of polymorphic assignment statements.

So, can we please have a bug fix for this? The above valgrind output should be helpful in this respect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants