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 078053858403 for ; Fri, 12 Apr 2024 19:08:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 078053858403 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 078053858403 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=1712948939; cv=none; b=E/cgLKWQ7W0hfU2hp09KoFCpernZB99EQ0bmewKcVDwzWjiPaBDsSK5qTzJAm7IM6hW5aKa+TPXaIIzi4LKEDtE3H1fyQPifIWeGc4b0QDpX8QRNM7vKPF3dV/5utCggJagqOe1TOEjEJvHwvDrrJiquyGPwoxbwZZlZSh+CZ74= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712948939; c=relaxed/simple; bh=RsWlHcoV2cV6mqiiNlx7thRru3zRXbXGLr7l0wq1jUg=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=KV8tejFyJhDlGL7CFSXbPykA0m6RLymiXDcZlj/+IXbXu5hxB+gZcbfjB7CPjK3+ahr5MC7R8fLapJMmLrBpVTCSpnZq2xUIZCEaYKwScOyN41owqYqrfN6jKaVR5lB1nZLxIeMrRLpIdQ1AshShwhWWToDN9SQ3atHq9DTTz0s= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712948936; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0uudCFDk+nk2VzU7CquPuK4pD9k65aivI61y4vRn36Q=; b=F52IDDY/fkzW/vOq45KoccNMhzXlFPQc2RbkjyiOj8rkLLbQHvkDInM8Gau0fpJOP1RVmO 9Oh0czM32GOfYAEHhiD0b99iBol36L0uBdJAWTQZSoyLnW3l2r0gffWWt3g2dPJxhBm6iM a2h78J8ao45IRKXTArqAkWm2mhEo+jE= Received: from mail-ot1-f70.google.com (mail-ot1-f70.google.com [209.85.210.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-570-ShrTs7ljPVeaFqAHZEgOcQ-1; Fri, 12 Apr 2024 15:08:55 -0400 X-MC-Unique: ShrTs7ljPVeaFqAHZEgOcQ-1 Received: by mail-ot1-f70.google.com with SMTP id 46e09a7af769-6eb666f8d8eso470349a34.1 for ; Fri, 12 Apr 2024 12:08:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712948934; x=1713553734; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=0uudCFDk+nk2VzU7CquPuK4pD9k65aivI61y4vRn36Q=; b=DBjq76F2npByeCE+dctP6Xzy+Bdtlglbs2WA+pbzpGn3GtPkdIxkwjJyPjo/V1MAoX CyIxsT+xG6dRZlA2RFhg8ofN6fcJQ9qCPOUlzqAqHE/KYhaxqg8RcFGpBO05tlDKSxTo nYbrS/fVn3PynHCD0IfSH49WrHuOv9fV+r+w/P9V8K35S4P+Y0I3dAGhonSKV06/3Wx0 Gd8cHoJvoouLoKgfaphc/JFXOzY6TZQ0mNmNf5V1MsNLoQice2KhoKQXpdOXuQChvI2M W7pr/c0VoSqd9Nx46lsq/PM+WisKCuqV9YxT8tE5pivOvkihTZPzmGNAPp3rekSMPhcl GIwA== X-Gm-Message-State: AOJu0Yy8Y8Ag3MYP492J4/J5PkwZ3RNrfr9V09e9CxVE54BrqdoqdhYZ 667mkQKw0zeaOPlBB4vbaUylEONnHMCGyfgOlncjtD+BGnJuV9uirY/HusQxamiVDn5gZvXWCZf XwkQhy8MUakxE2wxh3ooxlFhC+Zll4GQEnncv8XIV6y2X+pdgTuNyKH0= X-Received: by 2002:a05:6830:1692:b0:6ea:d43:9db2 with SMTP id k18-20020a056830169200b006ea0d439db2mr3580995otr.4.1712948934389; Fri, 12 Apr 2024 12:08:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHu0R+w4Ar0No7E82f6z46CNMUwbbZjlSWUOK3ZgIV4YMAyss5Rc9O+L0TdlxMZnR6ff4g18A== X-Received: by 2002:a05:6830:1692:b0:6ea:d43:9db2 with SMTP id k18-20020a056830169200b006ea0d439db2mr3580979otr.4.1712948934103; Fri, 12 Apr 2024 12:08:54 -0700 (PDT) Received: from [192.168.1.130] (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id i22-20020a05620a0a1600b0078d75b9fb37sm2752595qka.32.2024.04.12.12.08.53 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 12 Apr 2024 12:08:53 -0700 (PDT) Message-ID: Date: Fri, 12 Apr 2024 15:08:52 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] c++/modules: local class merging [PR99426] To: Patrick Palka Cc: gcc-patches@gcc.gnu.org, nathan@acm.org References: <20240227023734.2742095-1-ppalka@redhat.com> <9d4f1a3e-99ea-5146-639d-22add3e22301@idea> <63e9bd8a-f45c-46e1-a006-110284d7c54d@redhat.com> <1b682650-7ef5-429f-d738-ae189884191b@idea> <45179f66-242f-18f0-74e3-9be4d04d4d55@idea> <945b6320-a469-43c3-a32a-13819f14b971@redhat.com> <1a24b2c4-5d38-41b2-a290-5dea96ffa789@redhat.com> From: Jason Merrill In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-11.8 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,RCVD_IN_SORBS_WEB,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 4/12/24 14:39, Patrick Palka wrote: > On Fri, 12 Apr 2024, Jason Merrill wrote: > >> On 4/12/24 13:48, Patrick Palka wrote: >>> On Fri, 12 Apr 2024, Jason Merrill wrote: >>> >>>> On 4/12/24 10:35, Patrick Palka wrote: >>>>> On Wed, 10 Apr 2024, Jason Merrill wrote: >>>>> >>>>>> On 4/10/24 14:48, Patrick Palka wrote: >>>>>>> On Tue, 9 Apr 2024, Jason Merrill wrote: >>>>>>> >>>>>>>> On 3/5/24 10:31, Patrick Palka wrote: >>>>>>>>> On Tue, 27 Feb 2024, Patrick Palka wrote: >>>>>>>>> >>>>>>>>> Subject: [PATCH] c++/modules: local type merging [PR99426] >>>>>>>>> >>>>>>>>> One known missing piece in the modules implementation is merging >>>>>>>>> of >>>>>>>>> a >>>>>>>>> streamed-in local type (class or enum) with the corresponding >>>>>>>>> in-TU >>>>>>>>> version of the local type. This missing piece turns out to >>>>>>>>> cause a >>>>>>>>> hard-to-reduce use-after-free GC issue due to the entity_ary not >>>>>>>>> being >>>>>>>>> marked as a GC root (deliberately), and manifests as a >>>>>>>>> serialization >>>>>>>>> error on stream-in as in PR99426 (see comment #6 for a >>>>>>>>> reduction). >>>>>>>>> It's >>>>>>>>> also reproducible on trunk when running the xtreme-header tests >>>>>>>>> without >>>>>>>>> -fno-module-lazy. >>>>>>>>> >>>>>>>>> This patch makes us merge such local types according to their >>>>>>>>> position >>>>>>>>> within the containing function's definition, analogous to how we >>>>>>>>> merge >>>>>>>>> FIELD_DECLs of a class according to their index in the >>>>>>>>> TYPE_FIELDS >>>>>>>>> list. >>>>>>>>> >>>>>>>>> PR c++/99426 >>>>>>>>> >>>>>>>>> gcc/cp/ChangeLog: >>>>>>>>> >>>>>>>>> * module.cc (merge_kind::MK_local_type): New enumerator. >>>>>>>>> (merge_kind_name): Update. >>>>>>>>> (trees_out::chained_decls): Move BLOCK-specific handling >>>>>>>>> of DECL_LOCAL_DECL_P decls to ... >>>>>>>>> (trees_out::core_vals) : ... here. Stream >>>>>>>>> BLOCK_VARS manually. >>>>>>>>> (trees_in::core_vals) : Stream BLOCK_VARS >>>>>>>>> manually. Handle deduplicated local types.. >>>>>>>>> (trees_out::key_local_type): Define. >>>>>>>>> (trees_in::key_local_type): Define. >>>>>>>>> (trees_out::get_merge_kind) : Return >>>>>>>>> MK_local_type for a local type. >>>>>>>>> (trees_out::key_mergeable) : Use >>>>>>>>> key_local_type. >>>>>>>>> (trees_in::key_mergeable) : Likewise. >>>>>>>>> (trees_in::is_matching_decl): Be flexible with type mismatches >>>>>>>>> for local entities. >>>>>>>>> >>>>>>>>> diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc >>>>>>>>> index 80b63a70a62..d9e34e9a4b9 100644 >>>>>>>>> --- a/gcc/cp/module.cc >>>>>>>>> +++ b/gcc/cp/module.cc >>>>>>>>> @@ -6714,7 +6720,37 @@ trees_in::core_vals (tree t) >>>>>>>>> case BLOCK: >>>>>>>>> t->block.locus = state->read_location (*this); >>>>>>>>> t->block.end_locus = state->read_location (*this); >>>>>>>>> - t->block.vars = chained_decls (); >>>>>>>>> + >>>>>>>>> + for (tree *chain = &t->block.vars;;) >>>>>>>>> + if (tree decl = tree_node ()) >>>>>>>>> + { >>>>>>>>> + /* For a deduplicated local type or enumerator, chain the >>>>>>>>> + duplicate decl instead of the canonical in-TU decl. >>>>>>>>> Seeing >>>>>>>>> + a duplicate here means the containing function whose >>>>>>>>> body >>>>>>>>> + we're streaming in is a duplicate too, so we'll end up >>>>>>>>> + discarding this BLOCK (and the rest of the duplicate >>>>>>>>> function >>>>>>>>> + body) anyway. */ >>>>>>>>> + if (is_duplicate (decl)) >>>>>>>>> + decl = maybe_duplicate (decl); >>>>>>>>> + else if (DECL_IMPLICIT_TYPEDEF_P (decl) >>>>>>>>> + && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))) >>>>>>>>> + { >>>>>>>>> + tree tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); >>>>>>>>> + if (DECL_TEMPLATE_RESULT (tmpl) == decl && >>>>>>>>> is_duplicate >>>>>>>>> (tmpl)) >>>>>>>>> + decl = DECL_TEMPLATE_RESULT (maybe_duplicate >>>>>>>>> (tmpl)); >>>>>>>>> + } >>>>>>>> >>>>>>>> This seems like a lot of generally-applicable code for finding the >>>>>>>> duplicate, >>>>>>>> which other calls to maybe_duplicate/odr_duplicate don't use. If >>>>>>>> the >>>>>>>> template >>>>>>>> is a duplicate, why isn't its result? If there's a good reason >>>>>>>> for >>>>>>>> that, >>>>>>>> should this template handling go into maybe_duplicate? >>>>>>> >>>>>>> Ah yeah, that makes sense. >>>>>>> >>>>>>> Some context: IIUC modules treats the TEMPLATE_DECL instead of the >>>>>>> DECL_TEMPLATE_RESULT as the canonical decl, which in turn means >>>>>>> we'll >>>>>>> register_duplicate only the TEMPLATE_DECL. But BLOCK_VARS never >>>>>>> contains >>>>>>> a TEMPLATE_DECL, always the DECL_TEMPLATE_RESULT (i.e. a TYPE_DECL), >>>>>>> hence the extra handling. >>>>>>> >>>>>>> Given that it's relatively more difficult to get at the >>>>>>> TEMPLATE_DECL >>>>>>> from the DECL_TEMPLATE_RESULT rather than vice versa, maybe we >>>>>>> should >>>>>>> just register both as duplicates from register_duplicate? That way >>>>>>> callers can just simply pass the DECL_TEMPLATE_RESULT to >>>>>>> maybe_duplicate >>>>>>> and it'll do the right thing. >>>>>> >>>>>> Sounds good. >>>>>> >>>>>>>>> @@ -10337,6 +10373,83 @@ trees_in::fn_parms_fini (int tag, tree >>>>>>>>> fn, >>>>>>>>> tree >>>>>>>>> existing, bool is_defn) >>>>>>>>> } >>>>>>>>> } >>>>>>>>> +/* Encode into KEY the position of the local type (class >>>>>>>>> or >>>>>>>>> enum) >>>>>>>>> + declaration DECL within FN. The position is encoded as the >>>>>>>>> + index of the innermost BLOCK (numbered in BFS order) along >>>>>>>>> with >>>>>>>>> + the index within its BLOCK_VARS list. */ >>>>>>>> >>>>>>>> Since we already set DECL_DISCRIMINATOR for mangling, could we use >>>>>>>> it+name >>>>>>>> for >>>>>>>> the key as well? >>>>>>> >>>>>>> We could (and IIUc that'd be more robust to ODR violations), but >>>>>>> wouldn't it mean we'd have to do a linear walk over all BLOCK_VARs >>>>>>> of >>>>>>> all BLOCKS in order to find the one with the matching >>>>>>> name+discriminator? That'd be slower than the current approach >>>>>>> which >>>>>>> lets us skip to the correct BLOCK and walk only its BLOCK_VARS. >>>>>> >>>>>> Ah, good point. How about block number + name instead of the index? >>>>> >>>>> It seems DECL_DISCRIMINATOR is only set at instantiation time and so for >>>>> local types from a function template pattern the field is empty, which >>>>> means we can't use it as the key in general :/ >>>> >>>> I meant just block number and name, without DECL_DISCRIMINATOR. Just >>>> using >>>> the name instead of an index in BLOCK_VARS. >>> >>> Ah, I think that'd be enough for named local types, but what about >>> anonymous local types? IIUC without DECL_DISCRIMINATOR we wouldn't be >>> able to reliably distinguisth between multiple anonymous local types >>> defined in the same block, since their identifiers aren't stable given >>> that they're based off of a global counter (and so sensitive to #include >>> order) :( >> >> Good point. But I'd still think to merge based on name if we have one; as you >> said above, to be more robust to ODR violations. > > Sounds good. > >> >> If the imported fn has a local class that the included header didn't, would we >> get the same problem? > > IIUC yes, all the intra-block indexes would be off by one in that case > and deduplicatation would fail or we'd deduplicate distinct types. > > Here's an incremental diff for the updated patch. The augmented > testcase triggered a latent qsort checking failure in depset_cmp > that was straightforwardly fixed: OK. Jason