From patchwork Wed Nov 3 16:28:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 47015 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 164E23858018 for ; Wed, 3 Nov 2021 16:40:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 164E23858018 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1635957657; bh=qKRPFTqNFH5Hk4bpOff+Jem3rqdIzBbQVE/H+JTfspU=; h=To:Subject:In-Reply-To:References:Date:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=trEHEQptn+c1blsiGjVTemaVrcLhmrI1QQdNO4N/hltLddFirzqTDaeB2IncZIK8i iCBtED2ixFwFfSizH2Da7UtIJRvhOpeLaXpH1EkZE9fZOmH7+GmfDI49ArL/JVIVR+ tlgQujb5WFFH3++h8soLY/Hp8IFlTWY7Q8TIXxVk= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org 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 21801385840A for ; Wed, 3 Nov 2021 16:28:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 21801385840A Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-250-EuI_jWiHOGuwDcrggiBNNQ-1; Wed, 03 Nov 2021 12:28:53 -0400 X-MC-Unique: EuI_jWiHOGuwDcrggiBNNQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CFDFC18125C2; Wed, 3 Nov 2021 16:28:52 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.3]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1768968D7D; Wed, 3 Nov 2021 16:28:49 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [PATCH 3/4] libgcc: Split FDE search code from PT_GNU_EH_FRAME lookup In-Reply-To: References: X-From-Line: 53daedec153e3bf9b1a9c14f61cfe23385de80c9 Mon Sep 17 00:00:00 2001 Message-Id: <53daedec153e3bf9b1a9c14f61cfe23385de80c9.1635955148.git.fweimer@redhat.com> Date: Wed, 03 Nov 2021 17:28:48 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.7 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_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Florian Weimer via Libc-alpha From: Florian Weimer Reply-To: Florian Weimer Cc: Jakub Jelinek , libc-alpha@sourceware.org, Jason Merrill Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" This allows switching to a different implementation for PT_GNU_EH_FRAME lookup in a subsequent commit. This moves some of the PT_GNU_EH_FRAME parsing out of the glibc loader lock that is implied by dl_iterate_phdr. However, the FDE is already parsed outside the lock before this change, so this does not introduce additional crashes in case of a concurrent dlclose. libunwind/ChangeLog * unwind-dw2-fde-dip.c (struct unw_eh_callback_data): Add hdr. Remove func, ret. (struct find_fde_tail_result): New. (find_fde_tail): New function. Split from _Unwind_IteratePhdrCallback. (_Unwind_Find_FDE): Add call to find_fde_tail. --- libgcc/unwind-dw2-fde-dip.c | 91 +++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 39 deletions(-) diff --git a/libgcc/unwind-dw2-fde-dip.c b/libgcc/unwind-dw2-fde-dip.c index 3f302826d2d..272c0ec46c0 100644 --- a/libgcc/unwind-dw2-fde-dip.c +++ b/libgcc/unwind-dw2-fde-dip.c @@ -113,8 +113,7 @@ struct unw_eh_callback_data #if NEED_DBASE_MEMBER void *dbase; #endif - void *func; - const fde *ret; + const struct unw_eh_frame_hdr *hdr; int check_cache; }; @@ -197,10 +196,6 @@ _Unwind_IteratePhdrCallback (struct dl_phdr_info *info, size_t size, void *ptr) #else _Unwind_Ptr load_base; #endif - const unsigned char *p; - const struct unw_eh_frame_hdr *hdr; - _Unwind_Ptr eh_frame; - struct object ob; _Unwind_Ptr pc_low = 0, pc_high = 0; struct ext_dl_phdr_info @@ -348,10 +343,8 @@ _Unwind_IteratePhdrCallback (struct dl_phdr_info *info, size_t size, void *ptr) return 0; /* Read .eh_frame_hdr header. */ - hdr = (const struct unw_eh_frame_hdr *) + data->hdr = (const struct unw_eh_frame_hdr *) __RELOC_POINTER (p_eh_frame_hdr->p_vaddr, load_base); - if (hdr->version != 1) - return 1; #ifdef CRT_GET_RFIB_DATA # if defined __i386__ || defined __nios2__ @@ -383,12 +376,34 @@ _Unwind_IteratePhdrCallback (struct dl_phdr_info *info, size_t size, void *ptr) # endif #endif - _Unwind_Ptr dbase = unw_eh_callback_data_dbase (data); + return 1; +} + +/* Result type of find_fde_tail below. */ +struct find_fde_tail_result +{ + const fde *entry; + void *func; +}; + +/* Find the FDE for the program counter PC, in a previously located + PT_GNU_EH_FRAME data region. */ +static struct find_fde_tail_result +find_fde_tail (_Unwind_Ptr pc, + const struct unw_eh_frame_hdr *hdr, + _Unwind_Ptr dbase) +{ + const unsigned char *p = (const unsigned char *) (hdr + 1); + _Unwind_Ptr eh_frame; + struct object ob; + + if (hdr->version != 1) + return (struct find_fde_tail_result) { NULL, }; + p = read_encoded_value_with_base (hdr->eh_frame_ptr_enc, base_from_cb_data (hdr->eh_frame_ptr_enc, dbase), - (const unsigned char *) (hdr + 1), - &eh_frame); + p, &eh_frame); /* We require here specific table encoding to speed things up. Also, DW_EH_PE_datarel here means using PT_GNU_EH_FRAME start @@ -404,7 +419,7 @@ _Unwind_IteratePhdrCallback (struct dl_phdr_info *info, size_t size, void *ptr) p, &fde_count); /* Shouldn't happen. */ if (fde_count == 0) - return 1; + return (struct find_fde_tail_result) { NULL, }; if ((((_Unwind_Ptr) p) & 3) == 0) { struct fde_table { @@ -419,9 +434,9 @@ _Unwind_IteratePhdrCallback (struct dl_phdr_info *info, size_t size, void *ptr) _Unwind_Ptr range; mid = fde_count - 1; - if (data->pc < table[0].initial_loc + data_base) - return 1; - else if (data->pc < table[mid].initial_loc + data_base) + if (pc < table[0].initial_loc + data_base) + return (struct find_fde_tail_result) { NULL, }; + else if (pc < table[mid].initial_loc + data_base) { lo = 0; hi = mid; @@ -429,9 +444,9 @@ _Unwind_IteratePhdrCallback (struct dl_phdr_info *info, size_t size, void *ptr) while (lo < hi) { mid = (lo + hi) / 2; - if (data->pc < table[mid].initial_loc + data_base) + if (pc < table[mid].initial_loc + data_base) hi = mid; - else if (data->pc >= table[mid + 1].initial_loc + data_base) + else if (pc >= table[mid + 1].initial_loc + data_base) lo = mid + 1; else break; @@ -445,10 +460,11 @@ _Unwind_IteratePhdrCallback (struct dl_phdr_info *info, size_t size, void *ptr) f_enc_size = size_of_encoded_value (f_enc); read_encoded_value_with_base (f_enc & 0x0f, 0, &f->pc_begin[f_enc_size], &range); - if (data->pc < table[mid].initial_loc + data_base + range) - data->ret = f; - data->func = (void *) (table[mid].initial_loc + data_base); - return 1; + void *func = (void *) (table[mid].initial_loc + data_base); + if (pc < table[mid].initial_loc + data_base + range) + return (struct find_fde_tail_result) { f, func }; + else + return (struct find_fde_tail_result) { NULL, func }; } } @@ -461,18 +477,18 @@ _Unwind_IteratePhdrCallback (struct dl_phdr_info *info, size_t size, void *ptr) ob.u.single = (fde *) eh_frame; ob.s.i = 0; ob.s.b.mixed_encoding = 1; /* Need to assume worst case. */ - data->ret = linear_search_fdes (&ob, (fde *) eh_frame, (void *) data->pc); - if (data->ret != NULL) + const fde *entry = linear_search_fdes (&ob, (fde *) eh_frame, (void *) pc); + if (entry != NULL) { _Unwind_Ptr func; - unsigned int encoding = get_fde_encoding (data->ret); + unsigned int encoding = get_fde_encoding (entry); read_encoded_value_with_base (encoding, base_from_cb_data (encoding, dbase), - data->ret->pc_begin, &func); - data->func = (void *) func; + entry->pc_begin, &func); + return (struct find_fde_tail_result) { entry, (void *) func }; } - return 1; + return (struct find_fde_tail_result) { NULL, }; } const fde * @@ -489,24 +505,21 @@ _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases) #if NEED_DBASE_MEMBER data.dbase = NULL; #endif - data.func = NULL; - data.ret = NULL; data.check_cache = 1; - if (dl_iterate_phdr (_Unwind_IteratePhdrCallback, &data) < 0) + if (dl_iterate_phdr (_Unwind_IteratePhdrCallback, &data) <= 0) return NULL; - if (data.ret) + _Unwind_Ptr dbase = unw_eh_callback_data_dbase (&data); + struct find_fde_tail_result result = find_fde_tail ((_Unwind_Ptr) pc, + data.hdr, dbase); + if (result.entry != NULL) { bases->tbase = NULL; -#if NEED_DBASE_MEMBER - bases->dbase = data.dbase; -#else - bases->dbase = NULL; -#endif - bases->func = data.func; + bases->dbase = (void *) dbase; + bases->func = result.func; } - return data.ret; + return result.entry; } #else