valgrind on a Fortran program

An allocatable variable is deallocated for you at the end of its scope; a pointer is not. A pointer target that is never freed is a leak, and a leak in a program that runs once is harmless until the program is a server or a loop over ten thousand files. valgrind finds these, and it also finds a read or a write outside an allocated block, which Fortran's own checks cover only for the arrays they know about. This lesson runs the same two programs through valgrind --leak-check=full and reads the report.

A leak valgrind finds

The program allocates an array through a pointer, fills it and prints the sum. There is no DEALLOCATE at the end: a pointer is the programmer's memory, and Fortran does not free it when the program ends.

The file leak.f90:

program leak implicit none real, pointer :: data(:) integer :: i allocate(data(4000)) do i = 1, 4000 data(i) = real(i) end do print '(a,f12.1)', 'sum = ', sum(data) end program leak

gfortran -Wall -Wextra -std=f2018 -g leak.f90 -o leak
./leak
valgrind --leak-check=full ./leak

sum =   8002000.0
==518== Memcheck, a memory error detector
==518== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
==518== Using Valgrind-3.25.1 and LibVEX; rerun with -h for copyright info
==518== Command: ./leak
==518==
sum =   8002000.0
==518==
==518== HEAP SUMMARY:
==518==     in use at exit: 16,000 bytes in 1 blocks
==518==   total heap usage: 22 allocs, 21 frees, 26,654 bytes allocated
==518==
==518== 16,000 bytes in 1 blocks are definitely lost in loss record 1 of 1
==518==    at 0x48B7733: malloc (vg_replace_malloc.c:446)
==518==    by 0x40012BE: MAIN__ (leak.f90:6)
==518==    by 0x40014E1: main (leak.f90:11)
==518==
==518== LEAK SUMMARY:
==518==    definitely lost: 16,000 bytes in 1 blocks
==518==    indirectly lost: 0 bytes in 0 blocks
==518==      possibly lost: 0 bytes in 0 blocks
==518==    still reachable: 0 bytes in 0 blocks
==518==         suppressed: 0 bytes in 0 blocks
==518==
==518== For lists of detected and suppressed errors, rerun with: -s
==518== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

Four thousand single-precision reals are 16,000 bytes, and the record names leak.f90:6: the ALLOCATE whose block was never freed. The frames run from malloc inside the C library through the Fortran main program, which is why the build carries -g: without it the report has addresses and no line.

The same program, freed

One statement at the end makes the summary clean, and everything else in the report stays the same:

The file clean.f90:

program clean implicit none real, pointer :: data(:) integer :: i allocate(data(4000)) do i = 1, 4000 data(i) = real(i) end do print '(a,f12.1)', 'sum = ', sum(data) deallocate(data) end program clean

gfortran -Wall -Wextra -std=f2018 -g clean.f90 -o clean
valgrind --leak-check=full ./clean

==545== Command: ./clean
==545==
sum =   8002000.0
==545==
==545== HEAP SUMMARY:
==545==     in use at exit: 0 bytes in 0 blocks
==545==   total heap usage: 22 allocs, 22 frees, 26,654 bytes allocated
==545==
==545== All heap blocks were freed -- no leaks are possible
==545==
==545== For lists of detected and suppressed errors, rerun with: -s
==545== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

22 allocs, 22 frees is the line to look at first: the counts match, and the summary says so in words. A program with no leak but with an invalid access still reports the access, so a clean ERROR SUMMARY is the one number a test script should check.

An invalid read

The next program reads one element past the end of a heap block. It prints a value, a zero here, and exits 0; valgrind reports the access and the block it went past:

The file badread.f90:

program badread implicit none integer, pointer :: a(:) integer :: i allocate(a(10)) a = 7 i = 11 print '(a,i0)', 'a(11) = ', a(i) deallocate(a) end program badread

gfortran -Wall -Wextra -std=f2018 -g badread.f90 -o badread
valgrind ./badread

==529== Memcheck, a memory error detector
==529== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
==529== Using Valgrind-3.25.1 and LibVEX; rerun with -h for copyright info
==529== Command: ./badread
==529==
==529== Invalid read of size 4
==529==    at 0x4BC2D92: ??? (in /usr/lib/libgfortran.so.5.0.0)
==529==    by 0x4BC4743: ??? (in /usr/lib/libgfortran.so.5.0.0)
==529==    by 0x4BBD26F: ??? (in /usr/lib/libgfortran.so.5.0.0)
==529==    by 0x4BBD644: ??? (in /usr/lib/libgfortran.so.5.0.0)
==529==    by 0x400145F: MAIN__ (badread.f90:9)
==529==    by 0x4001509: main (badread.f90:11)
==529==  Address 0x4ca6c88 is 0 bytes after a block of size 40 alloc'd
==529==    at 0x48B7733: malloc (vg_replace_malloc.c:446)
==529==    by 0x40012DE: MAIN__ (badread.f90:6)
==529==    by 0x4001509: main (badread.f90:11)
==529==
a(11) = 0
==529==
==529== HEAP SUMMARY:
==529==     in use at exit: 0 bytes in 0 blocks
==529==   total heap usage: 22 allocs, 22 frees, 10,691 bytes allocated
==529==
==529== All heap blocks were freed -- no leaks are possible
==529==
==529== For lists of detected and suppressed errors, rerun with: -s
==529== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

The read is size 4 because the element is a four-byte integer, and the address is 0 bytes after a block of size 40: ten integers, and this is the eleventh. The line badread.f90:9 is the print, and badread.f90:6 is the allocation the block belonged to. The heap is clean, so a reader who only looks at the leak summary misses the bug entirely.

The check of the previous lesson finds the same bug and stops at it, which is the cheaper way to see it:

gfortran -Wall -Wextra -std=f2018 -g -fcheck=all badread.f90 -o badread_check
./badread_check

At line 9 of file badread.f90
Fortran runtime error: Index '11' of dimension 1 of array 'a' above upper bound of 10

Error termination. Backtrace:
#0  0x7ab2aa027430 in ???
#1  0x7ab2aa028076 in ???
#2  0x7ab2aa02863b in ???
#3  0x5efd7a1ea601 in badread
  at /tmp/w116/badread.f90:9
#4  0x5efd7a1ea6f6 in main
  at /tmp/w116/badread.f90:11

valgrind earns its place where the check cannot go: a block allocated by a C function and handed over as a c_ptr, a string copied with the wrong length, a structure whose components were freed twice. It also finds a leak, which no Fortran run-time check reports at all, because a leak is not an error the language defines.

Reading the report

The four loss kinds are a scale from certain to harmless. definitely lost is a block with no pointer left to it, and it is the real leak. indirectly lost is a block reachable only from a block that is itself lost, so it counts as part of the same leak. possibly lost is a block with a pointer that does not point at its start, which is common with an interior pointer and usually not a leak. still reachable is a block a global variable still points at when the program ends; library caches live there and it is not a bug.

Two options make the report usable in a script. --error-exitcode=1 makes valgrind return 1 when it found anything, so a test runner can treat the run as failed without parsing the text. --track-origins=yes follows an uninitialised value back to where it was created, which costs time and is worth it when the message is Conditional jump or move depends on uninitialised value. And -q prints only the errors and the summary, which keeps a log short.

What valgrind costs

Every instruction runs on a simulated processor, so valgrind is roughly an order of magnitude slower, and a short program measures its startup rather than its work. Five runs of each on this machine, the sum program of the first section built with -g:

./clean                min 0.0007 s   median 0.0007 s
valgrind -q ./clean   min 1.0879 s   median 1.0922 s

The second figure is almost all valgrind starting up: 1.09 s against a program that finishes in under a millisecond. That is the reason valgrind belongs in a nightly test run rather than in the loop of a developer who rebuilds every minute, and the reason the shorter checks of the two previous lessons come first.

Rules for valgrind

Build with -g and without stripping, or the report has no line numbers. Run the test program, not the release binary, and give the run the input it needs, because valgrind only sees the code that executes. Read the first error rather than the summary: a program can leak nothing and still be wrong. And pair it with -fcheck=all, which is faster, catches the array cases and stops at them; valgrind is for the memory that Fortran does not describe.

A finaliser runs on DEALLOCATE and at the end of a scope, which matters here: a type with an allocatable component frees that component when the type is deallocated, so a leak in an object-oriented program is usually a pointer or an ALLOCATE in a procedure that returned early.

Exercises

  1. Build leak.f90 without -g and run valgrind. Which part of the leak record is missing?
  2. Run valgrind --leak-check=full --error-exitcode=1 ./leak; echo $?. Which status does the shell report, and what does the same command print for ./clean?
  3. Change badread.f90 to write a(11) = 7 instead of reading it. What does valgrind say, and does the program still finish?
  4. Add data = 0.0 before the loop in clean.f90 and run valgrind with --track-origins=yes. Does the report change? Why?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Why does a pointer target leak when an allocatable does not?

    Answer

    An allocatable is deallocated at the end of its scope, a pointer is not

    A pointer is the programmer's memory.

  2. Which valgrind option prints the loss records?

    Answer

    --leak-check=full

    Without it valgrind prints the summary only.

  3. What does the leak record name?

    Answer

    The ALLOCATE line the block came from

    leak.f90:6 is the allocate.

  4. What does definitely lost mean?

    Answer

    A block with no pointer left to it at exit

    That is the real leak.

  5. What does still reachable mean?

    Answer

    A block a live variable still points at when the program ends

    Library caches live there; it is not a bug.

  6. What does the invalid read of badread.f90 say about the address?

    Answer

    It is 0 bytes after a block of size 40

    Ten integers of four bytes, and this is the eleventh.

  7. Why is -g needed for a useful valgrind report?

    Answer

    Without it the frames carry addresses and no source lines

    The Fortran frames are the ones that need names.

  8. What does --error-exitcode=1 do?

    Answer

    Makes valgrind return 1 when it found anything

    A script can then use the status.

  9. Which tool is the cheaper first check for an out-of-bounds array access?

    Answer

    -fcheck=all, which stops at the line

    valgrind is for memory Fortran does not describe.

  10. What did the five runs show about valgrind on a short program?

    Answer

    The 1.09 s is almost all startup

    The program itself takes under a millisecond.

The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container, with valgrind 3.25.1 on a machine of 4 CPUs (Intel Core i3-6006U, 2.00 GHz). The timings are five runs of the same binary, min and median.

Article author: Arthur Isaev

Banner image