fortranvalgrindintel-fortranmemory-corruptionmemcheck

Fortran90 valgrind output help. Unexplained error happening at the beginning of code execution


I am currently writing a large non-linear solver for a nasty partial differential equation in Fortran90 as part of my research. I have run into an issue where I believe a memory corruption issue is plaguing my code and I am trying to track it down; to do so, I elected to use valgrind as this has worked for me in the past. Unfortunately, I am getting an unexplained error message that is occurring right at the beginning of my code execution, please see below:

==18257== Memcheck, a memory error detector
==18257== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==18257== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==18257== Command: ./JFNKsolver
==18257== 
==18257== Conditional jump or move depends on uninitialised value(s)
==18257==    at 0x6F7F7D: __intel_sse2_strcpy (in /home/cseinen/Documents/1_Thesis_Work/ThesisGit/Model/Source_Code/JFNKsolver)
==18257==    by 0x6AA1C0: for__open_proc (in /home/cseinen/Documents/1_Thesis_Work/ThesisGit/Model/Source_Code/JFNKsolver)
==18257==    by 0x67EF6C: for__open_default (in /home/cseinen/Documents/1_Thesis_Work/ThesisGit/Model/Source_Code/JFNKsolver)
==18257==    by 0x69878D: for_write_seq_lis (in /home/cseinen/Documents/1_Thesis_Work/ThesisGit/Model/Source_Code/JFNKsolver)
==18257==    by 0x65BA69: MAIN__ (JFNKsolver.f90:146)
==18257==    by 0x402DAD: main (in /home/cseinen/Documents/1_Thesis_Work/ThesisGit/Model/Source_Code/JFNKsolver)
==18257==  Uninitialised value was created by a stack allocation
==18257==    at 0x6AA07D: for__open_proc (in /home/cseinen/Documents/1_Thesis_Work/ThesisGit/Model/Source_Code/JFNKsolver)
==18257== 
==18257== Conditional jump or move depends on uninitialised value(s)
==18257==    at 0x6F7F7D: __intel_sse2_strcpy (in /home/cseinen/Documents/1_Thesis_Work/ThesisGit/Model/Source_Code/JFNKsolver)
==18257==    by 0x677DA5: for__add_to_lf_table (in /home/cseinen/Documents/1_Thesis_Work/ThesisGit/Model/Source_Code/JFNKsolver)
==18257==    by 0x6ABA13: for__open_proc (in /home/cseinen/Documents/1_Thesis_Work/ThesisGit/Model/Source_Code/JFNKsolver)
==18257==    by 0x67EF6C: for__open_default (in /home/cseinen/Documents/1_Thesis_Work/ThesisGit/Model/Source_Code/JFNKsolver)
==18257==    by 0x69878D: for_write_seq_lis (in /home/cseinen/Documents/1_Thesis_Work/ThesisGit/Model/Source_Code/JFNKsolver)
==18257==    by 0x65BA69: MAIN__ (JFNKsolver.f90:146)
==18257==    by 0x402DAD: main (in /home/cseinen/Documents/1_Thesis_Work/ThesisGit/Model/Source_Code/JFNKsolver)
==18257==  Uninitialised value was created by a stack allocation
==18257==    at 0x6AA07D: for__open_proc (in /home/cseinen/Documents/1_Thesis_Work/ThesisGit/Model/Source_Code/JFNKsolver)
==18257== 

I got this error by running my code with valgrind --track-origins=yes ./JFNKsolver.

Now I know that, typically, these errors mean that there is an uninitialized variable being used somewhere, but the strange thing is that it is appearing before anything is really happening in my code and it points via by 0x65BA69: MAIN__ (JFNKsolver.f90:146) to a test print statement, i.e. print *, "Test" in the code below.

program JFNKsolver

use modelparams             
use initialization          
use forcing                 
use domain_routines         
use solver_routines         
use var_routines            
use csv_file                
use validation_routines     
use validsoln_routines      

implicit none

real(kind = dbl_kind) :: &
    time,       &  
    norm_test

integer(kind = int_kind) :: &
    nxT, nyT,   &   
    nxN, nyN,   &   
    nxU, nyU,   &   
    nxV, nyV,   &   
    nt,         &   
    nU_pnts,    &   
    nV_pnts,    &   
    nT_pnts,    &   
    nN_pnts,    &   
    nHT_pnts,   &   
    nHN_pnts,   &   
    si,         &   
    tmp_size,   &   
    i, j, ij        


integer(kind = int_kind), allocatable, dimension (:) ::  &
    indxUi,     indxUj,         &   
    indxVi,     indxVj,         &   
    indxTi,     indxTj,         &   
    indxNi,     indxNj,         &   
    haloTi,     haloTj,         &   
    haloNi,     haloNj              

real(kind = dbl_kind), allocatable, dimension (:) :: &
    Au,         res_NL,     u_update,       &   
    b,          b_0,        vpb_forc            

integer(kind = int_kind), allocatable, dimension (:,:,:) :: &
    ulmsk,      vlmsk,      &               
    uimsk,      vimsk,      &               
    Timsk,      Nimsk,      &               
    Tlmsk,      Nlmsk                       

real(kind = dbl_kind), allocatable, dimension (:,:) :: &
    ugrid,      vgrid,                              &   
    uResgrid,   vResgrid,                           &   
    uocn_u,     uocn_v,                             &   
    vocn_u,     vocn_v,                             &   
    uwnd_u,     uwnd_v,                             &   
    vwnd_u,     vwnd_v,                             &   
    h_u,        h_v,        h_T,                    &   
                            A_T,                    &   
                            P_T,        P_N,        &   
                            zeta_T,     zeta_N,     &   
                            eta_T,      eta_N,      &   
    Cw_u,   Cw_v,                                   &   
                            dist_T,     dist_N,     &   
                            dist_gx_T,              &   
                            dist_gy_T                   

print *, "Test"

Note that I have removed comments to clean this up and the kind variables are initialized in the modelparams module. Can anyone provide some insight into what might be causing this? Could it be linked to the modules that I am using via the use statements? I originally thought this was a problem with valgrind but now that I'm seeing the effects of memory corruption, and the fact that this is the only error I'm seeing, I've become wary of it.

Note: the reason I believe there is a memory corruption issue is because I am seeing a change in my solver's behaviour just by the presence of code that shouldn't change anything. It was a functionality I added to help verify the code and I've ran extensive tests on this.


Solution

  • Steve Lionel answers essentially the same problem over at the Intel Developer Zone.

    Here is the example program used:

    Program Main
      implicit none
      write(*,*) "123"
    End Program
    

    Compiled with:

    $ ifort -g main.f90
    

    Then called with:

    $ valgrind -v --track-origins=yes --leak-check=full ./a.out
    

    Valgrind returns essentially the same output seen above.

    Here is Steve's reply in full.

    Yes, you are correct. You have implicitly opened a file, which requires allocated memory. The file is not closed, so that memory remains allocated at the end of the program. Even if the file is closed (and there is an implicit close when the program exits, not all of the bookkeeping memory may be deallocated. valgrind doesn't understand Fortran.

    You may not be able to get rid of all of the valgrind complaints.

    So, if I were you, I would ignore these warnings and look at any others you may have.