Skip to content

Memory errors detected by memcheck (valgrind) #1890

Description

@DusanJovic-NOAA

When I use the memcheck tool (valgrind) to check for memory errors in the ufs-weather-model, it detects these three errors in FMS:

==2308066== Conditional jump or move depends on uninitialised value(s)
==2308066==    at 0x4509B34: __mpp_mod_MOD_mpp_scatter_pelist_real8_gen_3d (mpp_scatter.fh:86)
==2308066==    by 0x450C0F9: __mpp_mod_MOD_mpp_scatter_pelist_real8_gen_2d (mpp_scatter.fh:63)
==2308066==    by 0x442FEDC: __fms_netcdf_domain_io_mod_MOD_domain_read_2d (domain_read.inc:261)
==2308066==    by 0x39D4CE0: __mom_io_infra_MOD_read_field_2d (MOM_io_infra.F90:907)
==2308066==    by 0x2D09994: __mom_io_MOD_mom_read_data_2d (MOM_io.F90:2217)
==2308066==    by 0x2EF73EA: __mom_grid_initialize_MOD_set_grid_metrics_from_mosaic (MOM_grid_initialize.F90:214)
==2308066==    by 0x2F00B8C: __mom_grid_initialize_MOD_set_grid_metrics (MOM_grid_initialize.F90:92)
==2308066==    by 0x2EDE9FF: __mom_fixed_initialization_MOD_mom_initialize_fixed (MOM_fixed_initialization.F90:82)
==2308066==    by 0x26AE5CA: __mom_MOD_initialize_mom (MOM.F90:2861)
==2308066==    by 0x26328B9: __mom_ocean_model_nuopc_MOD_ocean_model_init (mom_ocean_model_nuopc.F90:290)
==2308066==    by 0x25C0C9B: __mom_cap_mod_MOD_initializeadvertise (mom_cap.F90:755)
==2308066== Conditional jump or move depends on uninitialised value(s)
==2308066==    at 0x4514485: __mpp_mod_MOD_mpp_gather_pelist_real8_gen_3d (mpp_gather.fh:164)
==2308066==    by 0x45171B8: __mpp_mod_MOD_mpp_gather_pelist_real8_gen_2d (mpp_gather.fh:139)
==2308066==    by 0x441F8CF: __fms_netcdf_domain_io_mod_MOD_domain_write_2d (domain_write.inc:248)
==2308066==    by 0x39BEA00: __mom_io_infra_MOD_write_field_2d (MOM_io_infra.F90:1770)
==2308066==    by 0x2D470FD: __mom_io_file_MOD_write_field_2d_infra (MOM_io_file.F90:1157)
==2308066==    by 0x2CF9B94: __mom_io_MOD_mom_write_field_2d (MOM_io.F90:2913)
==2308066==    by 0x2F04C86: __mom_shared_initialization_MOD_write_ocean_geometry_file (MOM_shared_initialization.F90:1430)
==2308066==    by 0x26BD221: __mom_MOD_initialize_mom (MOM.F90:3137)
==2308066==    by 0x26328B9: __mom_ocean_model_nuopc_MOD_ocean_model_init (mom_ocean_model_nuopc.F90:290)
==2308066==    by 0x25C0C9B: __mom_cap_mod_MOD_initializeadvertise (mom_cap.F90:755)
==2308066== Conditional jump or move depends on uninitialised value(s)
==2308066==    at 0x4A2E748: __diag_output_mod_MOD_write_attribute_meta (diag_output.F90:730)
==2308066==    by 0x4A346D6: __diag_output_mod_MOD_write_axis_meta_data (diag_output.F90:368)
==2308066==    by 0x4355001: __diag_util_mod_MOD_opening_file (diag_util.F90:1830)
==2308066==    by 0x4350C30: __diag_util_mod_MOD_check_and_open (diag_util.F90:2173)
==2308066==    by 0x4351320: __diag_util_mod_MOD_diag_data_out (diag_util.F90:2083)
==2308066==    by 0x43031D0: __diag_manager_mod_MOD_closing_file (diag_manager.F90:4061)
==2308066==    by 0x4303439: __diag_manager_mod_MOD_diag_manager_end (diag_manager.F90:3990)
==2308066==    by 0x3993BB0: __mom_diag_manager_infra_MOD_mom_diag_manager_end (MOM_diag_manager_infra.F90:171)
==2308066==    by 0x2C0D762: __mom_diag_mediator_MOD_diag_mediator_end (MOM_diag_mediator.F90:3860)
==2308066==    by 0x2623EA7: __mom_ocean_model_nuopc_MOD_ocean_model_end (mom_ocean_model_nuopc.F90:796)
==2308066==    by 0x25A98D4: __mom_cap_mod_MOD_ocean_model_finalize (mom_cap.F90:2326)

In all three cases, the error is caused by passing either an unassociated pointer array or an unallocated allocatable array as an actual argument to a subroutine that has a corresponding dummy argument assumed shape intent(in) array.

In first two cases (mpp_gather and mpp_scatter) the simplest solution is to allocate a zero-sized array (data3D).

diff --git a/mpp/include/mpp_gather.fh b/mpp/include/mpp_gather.fh
index 6c40dc4e..7391eb3b 100644
--- a/mpp/include/mpp_gather.fh
+++ b/mpp/include/mpp_gather.fh
@@ -132,7 +132,8 @@ subroutine MPP_GATHER_PELIST_GEN_2D_(is, ie, js, je, pelist, array_seg, gather_d
    if (is_root_pe) then
      data3D(1:size(gather_data,1),1:size(gather_data,2),1:1) => gather_data
    else
-     data3D => null()
+     ! data3D => null()
+     allocate(data3D(0,0,0))
    endif

    call mpp_gather(is, ie, js, je, 1, pelist, arr3D, data3D, [dim_order, 3], is_root_pe, &
diff --git a/mpp/include/mpp_scatter.fh b/mpp/include/mpp_scatter.fh
index b0e5c5d1..4ca0edc0 100644
--- a/mpp/include/mpp_scatter.fh
+++ b/mpp/include/mpp_scatter.fh
@@ -57,7 +57,8 @@ subroutine MPP_SCATTER_PELIST_GEN_2D_(is, ie, js, je, pelist, array_seg, input_d
    if (is_root_pe) then
      data3D(1:size(input_data,1),1:size(input_data,2),1:1) => input_data
    else
-     data3D => null()
+     ! data3D => null()
+     allocate(data3D(0,0,0))
    endif

    call mpp_scatter(is, ie, js, je, 1, pelist, arr3D, data3D, [dim_order, 3], is_root_pe)

In the third case we can just skip calling the subroutine if the number of attributes is zero.

diff --git a/diag_manager/diag_output.F90 b/diag_manager/diag_output.F90
index cf8cd4f8..5f9f924e 100644
--- a/diag_manager/diag_output.F90
+++ b/diag_manager/diag_output.F90
@@ -365,10 +365,12 @@ CONTAINS
        endif

        !> Write additional axis attributes, from diag_axis_add_attribute calls
-       CALL write_attribute_meta(file_unit, num_attributes, attributes, err_msg, varname=axis_name, fileob=fileob)
-       IF ( LEN_TRIM(err_msg) .GT. 0 ) THEN
-          CALL error_mesg('diag_output_mod::write_axis_meta_data', TRIM(err_msg), FATAL)
-       END IF
+       if (num_attributes > 0) then
+          CALL write_attribute_meta(file_unit, num_attributes, attributes, err_msg, varname=axis_name, fileob=fileob)
+          IF ( LEN_TRIM(err_msg) .GT. 0 ) THEN
+             CALL error_mesg('diag_output_mod::write_axis_meta_data', TRIM(err_msg), FATAL)
+          END IF
+       endif

        !> Write additional attribute (calendar_type) for time axis ----
        !> @note calendar attribute is compliant with CF convention

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions