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 0DE693858CDB for ; Fri, 12 Apr 2024 17:18:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0DE693858CDB 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 0DE693858CDB 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=1712942298; cv=none; b=IyKHEactEJ++ma+ThrCUHKkN4AwIpqO28LAotb6ZGsdwhZRQtuL+w2o1v6yg205v7pq1+GxZ51NAL/qkDSKYwXMgJF1TMhCp/OPH3hXw99oqEaxipHPcAj82zYAoHqOnQ4VbIBoa4uF0zbDyu4KRx1OOLX7PlFETR+iVVgZHSK4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712942298; c=relaxed/simple; bh=525NzLoUzVFoRlaUB99NOMwNjRYgrUzeh4wodb2e9rA=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=vHxI0mcTO1XCaXrexx47aIUyWD++F0Z0UdZY1J5Mp8awib1B8YH09RWlzD/Ce3N27hVE5LEXqHkYCg2SnRaMfkB/ljC4FfaVYWpMOkG4ZVsGuzXlS/0BpLoTPwkQ84+9Hyq+8Wgo3L3Naua8zTn6lnaMZh2pQOMXX91XMeS26fk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712942296; 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=/K9O4UBdU/UScdCPBetov7nO1uvR3NuPtQjKx9FMk+A=; b=E3UrvBG1P2ZIqj+L6PXRgF2Xz9kbTYNSG5g3+toIMj0ngDiJ0vOkrTaNS0HutT3nShD3np ylJtFLUirGhgUo0R5ceoFaOZDfSMv3Ww4gyc+Hr/Co96FCNXNDcHlEWIdecOoCK6cZuSDq bwMPHsWOoDPpxldleUwxFBNoDdPDWGU= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-306-S17qz2c_PZm9gjvEl98wKw-1; Fri, 12 Apr 2024 13:18:15 -0400 X-MC-Unique: S17qz2c_PZm9gjvEl98wKw-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-78ece8c1c56so98844985a.0 for ; Fri, 12 Apr 2024 10:18:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712942294; x=1713547094; 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=/K9O4UBdU/UScdCPBetov7nO1uvR3NuPtQjKx9FMk+A=; b=FFmPDT8h8T97U+eYEbdiDJZPtwgOo3iYV7tD8LeuOzmvmFXYr1/OymLJsRaM8JYu/D B2JTG+FiEbwiB5kFEmDxMoxx96y2RNSgBZBVukeOurYKZP/YqQCIlEM5OAvdcuumac9m jpPGSuNoN9RBrTVXiLVvHi9HrWD8FfJl+g6CoZ9c3+YXHEnl6rrkCuNBSlkN9qKqMs+c CqBgCiBTHi6Q72UDKjyK/48IJlIvVTPyxnsBZ6Wy6twKIuuQJ4bX0x/WO0PWbe3EmRIL WW6AxOI4zu56ZncNsd2eTkvNecdqeCJyKm5ZrHulwScC6Yk+IOBn7ihPfZ38sM2F4TP8 /9mg== X-Gm-Message-State: AOJu0Ywls6758NE8eI0n7peft581QZN4Pp32yy7JJboFLrOpvo+pqssL td2d+54EcJPd7GcaouMXnBectHyTeKx6ibeLnY/owSCbRGG64EwnspCjrjgj2XFNN4JMLiV+CgU o6VBc6SRdJuLhXJqDEX+SwNUFQi6pbnb5CCkMOOBtLUm61bO5aGhEi0Q= X-Received: by 2002:a05:6214:4019:b0:69b:4c2a:7644 with SMTP id kd25-20020a056214401900b0069b4c2a7644mr8894411qvb.7.1712942294637; Fri, 12 Apr 2024 10:18:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IED8D+sKiayk3YyzUqbGWsUlQ1+zXCk0dxlpjCexXBmIRfJKq/Fk3ex4EpA6rFOpFnF6nuw7w== X-Received: by 2002:a05:6214:4019:b0:69b:4c2a:7644 with SMTP id kd25-20020a056214401900b0069b4c2a7644mr8894379qvb.7.1712942294282; Fri, 12 Apr 2024 10:18:14 -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 de14-20020ad4584e000000b00696ae38c7bfsm2568491qvb.35.2024.04.12.10.18.13 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 12 Apr 2024 10:18:13 -0700 (PDT) Message-ID: <945b6320-a469-43c3-a32a-13819f14b971@redhat.com> Date: Fri, 12 Apr 2024 13:18:12 -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> From: Jason Merrill In-Reply-To: <45179f66-242f-18f0-74e3-9be4d04d4d55@idea> 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=-5.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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=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: 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. Jason