flang
flang copied to clipboard
Flang runtime uses uninitialized value, causing memory leak and segfault
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).