From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) by sourceware.org (Postfix) with ESMTPS id 7A7A33858D33 for ; Wed, 8 Mar 2023 18:19:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7A7A33858D33 Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=carnegiescience.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pj1-f46.google.com with SMTP id x34so17347147pjj.0 for ; Wed, 08 Mar 2023 10:19:07 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678299546; h=content-transfer-encoding:mime-version:organization:message-id:date :subject:to:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=KBg5Wx0SO2blbtGzeDv8p4DtgDvaHwpJsYBgT/kjZEQ=; b=OccMn0IIpF8IW3x8Rtm1eQv4xiKzfVQuXVlXVGFCGj+jXmYJF3XVQrP3+FTJw19l1W c7xHM/Sn/4KY0oI7TLmX3obczfOccxRQDaVucdhaV16wRffB9zuUYa2DRlaJ9VqUT9il fFgE7kySE49jP7qDBPUGT13SXSZ0cbux8VqItJj0DXwHexL3DIS+4OS4PVXkXes7NLBt BNJrSYgh7q6NG6o6O8T9juOHBKfUbf1iJpQy/rLcjVRvDQv/k/YMphgwPQ02mdIigef5 SVO+sdOPkJsWZf0DfdB4BJlaCh428yWm9BK1dgmZ9X6H0uCdM0Vqf5C6Ivgl5d/PGiSN 5sww== X-Gm-Message-State: AO0yUKUgpp43S0s24YxdEKzQbJ9/ZFpGKcuYo2UR0zW/jzpFom1riIZ1 O5ICHSsXII0yPu35LFyS7z8nPrcpYdo= X-Google-Smtp-Source: AK7set8Nonh5LYy3jWeS6LzcZbd1GTI8BokZhMkl7DFxN4zHvljKVJ1cfgE44wZZFC//8eCjrpx19Q== X-Received: by 2002:a17:902:d504:b0:19c:e842:a9e0 with SMTP id b4-20020a170902d50400b0019ce842a9e0mr24947095plg.16.1678299546143; Wed, 08 Mar 2023 10:19:06 -0800 (PST) Received: from abensonca-precision-7540.localnet (075-140-015-040.biz.spectrum.com. [75.140.15.40]) by smtp.gmail.com with ESMTPSA id iw3-20020a170903044300b0019ca68ef7c3sm10168122plb.74.2023.03.08.10.19.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Mar 2023 10:19:05 -0800 (PST) From: Andrew Benson To: fortran@gcc.gnu.org Subject: Segfault when using defined assignment Date: Wed, 08 Mar 2023 10:19:04 -0800 Message-ID: <3842436.AGO1ar0Fap@abensonca-precision-7540> Organization: Observatories of the Carnegie Institution for Science MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,KAM_DMARC_STATUS,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: I opened a PR on bugzilla for the following: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109066 The following code (compiled using current trunk), when run, causes a segfault, and valgrind complains about an invalid read. The code appears correct to me, and runs correctly (no segfault, no warnings from valgrind) when compiled with ifort. module Input_Parameters_Bug public type :: resourceManager integer :: counter=0 ! Remove this to resolve segfault contains procedure :: resourceManagerAssign generic :: assignment(=) => resourceManagerAssign end type resourceManager type hdf5Object private type (resourceManager) :: objectManager contains procedure :: openGroup =>IO_HDF5_Open_Group ! procedure :: h5Assign ! Add this defined assignment to avoid segfault. ! generic :: assignment(=) => h5Assign end type hdf5Object interface hdf5Object module procedure hdf5Constructor end interface hdf5Object type :: inputParameters private type(hdf5Object), pointer :: outputParameters => null() ! Make this allocatable instead of pointer to resolve segfault end type inputParameters interface inputParameters module procedure inputParametersConstructorNode end interface inputParameters contains subroutine resourceManagerAssign(to,from) implicit none class(resourceManager), intent( out) :: to class(resourceManager), intent(in ) :: from to%counter=from%counter+1 write (0,*) "ASSIGN",to%counter return end subroutine resourceManagerAssign function hdf5Constructor() result(self) implicit none type(hdf5Object) :: self return end function hdf5Constructor function IO_HDF5_Open_Group(inObject) result (self) implicit none type(hdf5Object) :: self class(hdf5Object), intent(in ) :: inObject write (0,*) "OPEN" return end function IO_HDF5_Open_Group subroutine h5Assign(to,from) implicit none class(hdf5Object), intent( out) :: to class(hdf5Object), intent(in ) :: from write (0,*) "ASSIGN H5" to%objectManager=from%objectManager return end subroutine h5Assign function inputParametersConstructorNode(outputParametersGroup) result(self) implicit none type(inputParameters) :: self type(hdf5Object ), intent(in ) :: outputParametersGroup allocate(self%outputParameters) write (0,*) "START" self%outputParameters=outputParametersGroup%openGroup() write (0,*) "STOP" return end function inputParametersConstructorNode end module Input_Parameters_Bug program Test_Parameters_Bug use :: Input_Parameters_Bug implicit none type(hdf5Object) :: outputFile type(inputParameters) :: testParameters outputFile=hdf5Object() write (0,*) "CALL" testParameters=inputParameters(outputFile) end program Test_Parameters_Bug $ gfortran bug.F90 -g $ valgrind --track-origins=yes ./a.out ==19625== Memcheck, a memory error detector ==19625== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==19625== Using Valgrind-3.17.0 and LibVEX; rerun with -h for copyright info ==19625== Command: ./a.out ==19625== ASSIGN 1 CALL START OPEN ==19625== Use of uninitialised value of size 8 ==19625== at 0x4012DF: __input_parameters_bug_MOD_inputparametersconstructornode (bug.F90:73) ==19625== by 0x4017D4: MAIN__ (bug.F90:87) ==19625== by 0x401812: main (bug.F90:81) ==19625== Uninitialised value was created by a stack allocation ==19625== at 0x401206: __input_parameters_bug_MOD_inputparametersconstructornode (bug.F90:67) ==19625== Program received signal SIGSEGV: Segmentation fault - invalid memory reference. Backtrace for this error: #0 0x4e161ef in ??? #1 0x4012df in __input_parameters_bug_MOD_inputparametersconstructornode at /data001/abenson/Galacticus/galacticus_gfortranFinalization/ bug.F90:73 #2 0x4017d4 in test_parameters_bug at /data001/abenson/Galacticus/galacticus_gfortranFinalization/ bug.F90:87 #3 0x401812 in main at /data001/abenson/Galacticus/galacticus_gfortranFinalization/ bug.F90:81 ==19625== ==19625== Process terminating with default action of signal 11 (SIGSEGV) ==19625== at 0x4E1613E: raise (in /home/abenson/Galacticus/Tools/lib/ libc-2.12.1.so) ==19625== by 0x4E161EF: ??? (in /home/abenson/Galacticus/Tools/lib/ libc-2.12.1.so) ==19625== by 0x4012DE: __input_parameters_bug_MOD_inputparametersconstructornode (bug.F90:73) ==19625== by 0x4017D4: MAIN__ (bug.F90:87) ==19625== by 0x401812: main (bug.F90:81) ==19625== ==19625== HEAP SUMMARY: ==19625== in use at exit: 5,448 bytes in 18 blocks ==19625== total heap usage: 22 allocs, 4 frees, 13,588 bytes allocated ==19625== ==19625== LEAK SUMMARY: ==19625== definitely lost: 0 bytes in 0 blocks ==19625== indirectly lost: 0 bytes in 0 blocks ==19625== possibly lost: 0 bytes in 0 blocks ==19625== still reachable: 5,448 bytes in 18 blocks ==19625== suppressed: 0 bytes in 0 blocks ==19625== Rerun with --leak-check=full to see details of leaked memory ==19625== ==19625== For lists of detected and suppressed errors, rerun with: -s ==19625== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4) Segmentation fault This segfault goes away if I: 1. Remove the "counter" variable from type "resourceManager" 2. Add a defined assignment for type "hdf5Object" 3. Change the "outputParameters" variable from a pointer to an allocatable. -- * Andrew Benson: https://abensonca.github.io * Galacticus: https://github.com/galacticusorg/galacticus