From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 241BB3858D38 for ; Wed, 1 May 2024 13:57:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 241BB3858D38 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 241BB3858D38 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714571865; cv=none; b=yFQ2UACe5hTWsVBHoClVXV73Z4RAM247oF2x26vuNQ+o1WjRsr9ACSeiHYwFLMWNKk6f2d+hnQ5zBEeLw3WUcR1CJeDvDkmJE+7ugGSyc5Ctstr4iB82Fc7G1Thx/TR31XKrqdH/W7m0tWLhzH2NLvMr8lt5aGiB4VwO0AvES+0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714571865; c=relaxed/simple; bh=IkgIoAgUM/G6M/Ja/QPujdoGA6xc3LfJehSV4PHwSro=; h=DKIM-Signature:From:Date:To:Subject:Message-ID:MIME-Version; b=b5WSrfbJ5sV2P5o2lrDvDuJiTTDto+GuJNjOiDQORhMvSiUKb44UKFhj1Mi1vhRTUR0otOzTNOQKUK0Aum3YCTbQv7qR+cg7A+O9jqu47sHHRkLcSynNeYCojwdX4tUxIUsNDnHIli4I1tzi6XAL9EVXRDvn65Fq4qg7yFniZec= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1714571862; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=NsWEgfbS+3ERDy4xhVpt6e91t8o1N9PdA4tPQNZdC2w=; b=Zxyyyg+u2Qkotjk/kizkhUksEEfcnSnF9XCQcEeChpbvT7SPVHVXDjT2NK68RlNRWw5y2p DHD+Np+7eZai0hRS30anuvveCcM/0uGM16xCDJ7uXwY69i6ucUUXTC20hCTeB3mETFCA6M XBJVB0H7/vFhxSsrwF2rZN43aha5PWM= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-626-zE0xfiOnNGeHvST-Kh4SDA-1; Wed, 01 May 2024 09:57:41 -0400 X-MC-Unique: zE0xfiOnNGeHvST-Kh4SDA-1 Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-7885dd31632so957828585a.0 for ; Wed, 01 May 2024 06:57:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714571860; x=1715176660; h=mime-version:references:message-id:in-reply-to:subject:cc:to:date :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NsWEgfbS+3ERDy4xhVpt6e91t8o1N9PdA4tPQNZdC2w=; b=U7zYYa1Tc5sXeIyUfpO3G81Qd7dEk+Yw+cIbt49GlVzpck0XpklyrLvtClXcaE2tRh BmDdyl6xkkVaBp1Jf7BF9VBXk/rRZ8i0ftYG2INikk8Nl66vXTsz+1PpkjlzD2BPt9YT Nz+ZJ3SYN3+8C8Fvd0XSLgUP1nfpxCVND+5kaxgBuaL/SYSrBScrT3H+jTY+t5MhsFXi N48RKLXvSYRiBATNWO0rs1Lve/UzysRsrJtp0+yRxQ3/UHZ5rPLbIR46f/x0m1fC1PNw hxSqEDfGpMcom7vyQJenJ511l3xJhN7drYOn6XzYTw4HpF0rBPrk26wVYP+q8PXKEGKF F90w== X-Gm-Message-State: AOJu0YwR2Hd25y5ZOWjmMb/77DOb7i8Gnd7SbQGutlCCeD9fXsrygWcF OFFzPT7EX0aSf69f+SSZ5sOReK+W9I8C09Z/VHUqpzpBny0wXzBWgxNZ7i5TqAJP9xueIDb/IZI j2YwThn5X43Czoo++pwDBuJgABQqm6/235RIOew358cWpJjXAeNvqDzE= X-Received: by 2002:ae9:f10a:0:b0:790:690c:ff5c with SMTP id k10-20020ae9f10a000000b00790690cff5cmr2523944qkg.58.1714571860429; Wed, 01 May 2024 06:57:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG6h/9uVFi83ilVM3If2qmfLFjIqMAklW54LtoYHsM24C6ScM5XTNNWVLxr2CEV00dvwjH2bQ== X-Received: by 2002:ae9:f10a:0:b0:790:690c:ff5c with SMTP id k10-20020ae9f10a000000b00790690cff5cmr2523917qkg.58.1714571859930; Wed, 01 May 2024 06:57:39 -0700 (PDT) Received: from [192.168.1.130] (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id sg18-20020a05620a935200b00790fedd9262sm2190248qkn.42.2024.05.01.06.57.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 06:57:39 -0700 (PDT) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Wed, 1 May 2024 09:57:38 -0400 (EDT) To: Nathaniel Shead cc: gcc-patches@gcc.gnu.org, Jason Merrill , Nathan Sidwell Subject: Re: [PATCH] c++/modules: Fix dangling pointer with imported_temploid_friends In-Reply-To: <6631c581.170a0220.3b821.1e9d@mx.google.com> Message-ID: References: <6631c581.170a0220.3b821.1e9d@mx.google.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,URIBL_BLACK autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Wed, 1 May 2024, Nathaniel Shead wrote: > Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk (and > later 14.2)? I don't think making it a GTY root is necessary but I felt > perhaps better to be safe than sorry. > > Potentially another approach would be to use DECL_UID instead like how > entity_map does; would that be preferable? > > -- >8 -- > > I got notified by Linaro CI and by checking testresults that there seems > to be some occasional failures in tpl-friend-4_b.C on some architectures > and standards modes since r15-59-gb5f6a56940e708. I haven't been able > to reproduce but looking at the backtrace I suspect the issue is that > we're adding to the 'imported_temploid_friend' map a decl that is > ultimately discarded, which then has its address reused by a later decl > causing a failure in the assert in 'set_originating_module'. > > This patch attempts to fix the issue in two ways: by ensuring that we > only store the decl if we know it's a new decl (and hence won't be > discarded), and by making the imported_temploid_friends map a GTY root > so that even if the decl does get discarded later the address isn't > reused. > > gcc/cp/ChangeLog: > > * module.cc (imported_temploid_friends): Mark GTY, and... > (init_modules): ...allocate from GGC. > (trees_in::decl_value): Only write to imported_temploid_friends > for new decls. > > Signed-off-by: Nathaniel Shead > --- > gcc/cp/module.cc | 7 ++++--- > 1 file changed, 4 insertions(+), 3 deletions(-) > > diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc > index 5b8ff5bc483..37d38bb9654 100644 > --- a/gcc/cp/module.cc > +++ b/gcc/cp/module.cc > @@ -2731,7 +2731,7 @@ static keyed_map_t *keyed_table; > need to be attached to the same module as the temploid. This maps > these decls to the temploid they are instantiated them, as there is > no other easy way to get this information. */ > -static hash_map *imported_temploid_friends; > +static GTY(()) hash_map *imported_temploid_friends; > > /********************************************************************/ > /* Tree streaming. The tree streaming is very specific to the tree > @@ -8327,7 +8327,8 @@ trees_in::decl_value () > if (TREE_CODE (inner) == FUNCTION_DECL > || TREE_CODE (inner) == TYPE_DECL) > if (tree owner = tree_node ()) > - imported_temploid_friends->put (decl, owner); > + if (is_new) > + imported_temploid_friends->put (decl, owner); Hmm, I'm not seeing this code path getting reached for tpl-friend-4_b.C. It seems we're instead adding to imported_temploid_friends from propagate_defining_module, during tsubst_friend_function. What seems to be happening is that we we first tsubst_friend_function 'foo' from TPL, and then we tsubst_friend_function 'foo' from DEF, which ends up calling duplicate_decls, which ggc_frees this 'foo' redeclaration that is still present in the imported_temploid_friends map. So I don't think marking imported_temploid_friends as a GC root would help with this situation. If we want to keep imported_temploid_friends as a tree -> tree map, I think we just need to ensure that a decl is removed from the map upon getting ggc_free'd from e.g. duplicate_decls. But it seems simpler to use DECL_UID as the key instead, since those never get reused even after the decl gets ggc_free'd IIUC. > > /* Regular typedefs will have a NULL TREE_TYPE at this point. */ > unsigned tdef_flags = 0; > @@ -20523,7 +20524,7 @@ init_modules (cpp_reader *reader) > entity_map = new entity_map_t (EXPERIMENT (1, 400)); > vec_safe_reserve (entity_ary, EXPERIMENT (1, 400)); > imported_temploid_friends > - = new hash_map (EXPERIMENT (1, 400)); > + = hash_map::create_ggc (EXPERIMENT (1, 400)); > } > > #if CHECKING_P > -- > 2.43.2 > >